简体   繁体   中英

How do I get the path from MATLAB?

I want to implement a GUI environment in MATLAB. 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
  3. They assign the text to the String property of the handles text2 , text3 or text4 .

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. 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. There should be an entry that looks like this:

物业检查员的外观

  • None of the handles text2 , text3 or text4 exist. So Matlab does not know where to assign the text. 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. You could check this by making sure fileread is working on your files with a simple non-GUI script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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