简体   繁体   English

如何从MATLAB获取路径?

[英]How do I get the path from MATLAB?

I want to implement a GUI environment in MATLAB. 我想在MATLAB中实现GUI环境。 I want to use the Browser button to load a file, then input the file into the code I want to use and output it. 我想使用“浏览器”按钮加载文件,然后将文件输入到要使用的代码中并输出。 Help. 救命。

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(~, ~, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.text2, 'String', fullpathname)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(~, ~, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.text3, 'String', fullpathname)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(~, ~, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.text4, 'String', fullpathname)

D1=load('text2');
D1=D1';
D1=reshape(D1,l1*l2,1);

%% D2,D3 매트릭스 direct데이터 파일

D2=load('text3');
D3=load('text4');

You used 你用过

  set(handles.text2, 'String', fullpathname)

so in the routine that needs the name, you can use 因此在需要名称的例程中,您可以使用

 get(handles.text2, 'String')

All three of your callbacks already do exactly what you want to do: 您的所有三个回调已经完全完成了您想做的事情:

  1. When the user pushes the button that has one of the callbacks assigned they open the file explorer where the user can select a file. 当用户按下分配了回调之一的按钮时,他们将打开文件浏览器,用户可以在其中选择文件。
  2. They save the path in a fullpathname variable 他们将路径保存在fullpathname变量中
  3. They assign the text to the String property of the handles text2 , text3 or text4 . 他们将文本分配给句柄text2text3text4String属性。

So now there are several reasons why this could fail to work as you expect: 因此,现在有几个原因导致此操作无法按预期工作的原因:

  • None of your callback functions is assigned to user interface element of your GUI. 您的回调函数均未分配给GUI的用户界面元素。 You can simply check this if you open your GUI in the Matlab GUIDE feature, right click on one of the buttons and check the property inspector. 如果您在Matlab GUIDE功能中打开GUI,只需右键单击其中一个按钮并检查属性检查器,就​​可以简单地进行检查。 There should be an entry that looks like this: 应该有一个看起来像这样的条目:

物业检查员的外观

  • None of the handles text2 , text3 or text4 exist. 句柄text2text3text4存在。 So Matlab does not know where to assign the text. 因此Matlab不知道在哪里分配文本。 You can get a overview of all elements by using the Object browser in the upper menu: 您可以使用上方菜单中的“对象”浏览器来获得所有元素的概述:

对象浏览器

  • fileread is not able to read the content of your files. fileread无法读取文件的内容。 You could check this by making sure fileread is working on your files with a simple non-GUI script. 您可以通过使用简单的非GUI脚本确保fileread在文件上正常工作来进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM