Help with loading data in GUI

13 views (last 30 days)
aurc89
aurc89 on 30 Sep 2014
Commented: aurc89 on 2 Oct 2014
I need help with an operation in a Matlab GUI code. I have a folder in my laptop where some files (.dat) are saved , and they are labeled like this '2DVIS_data_08_40fs.dat', '2DVIS_data_08_120fs.dat', '2DVIS_data_08_1320fs.dat' and so on: the only part that changes in their name is the number included between the last underscore '_' and the string 'fs' (40,120,1320...). What I want to do with my code is: 1) save my file in a variable (e.g. 'myfile') by pressing a push button (tag 'push1') and loading it from my folder; 2) show the name of the loaded file (e.g.'2DVIS_data_08_40fs.dat') in an edit text box (tag 'edit1'); 3) show in another edit text box (tag 'edit2') just the part of the string included between the last underscore '_' and 'fs', in this case 40; 4) be able to change the number in this second edit text box (e.g. from 40 to 120) and , with another push button (tag 'push2') load the file which is in the same folder as the previous one but having the number I wrote between the last underscore '_' and the last two letters 'fs' (in this case '2DVIS_data_08_120fs.dat')- of course only if it exists - ; its name should appear in the first edit text box as well ('2DVIS_data_08_120fs.dat'), and should overwrite the previous one in the variable 'myfile'. I really don't know how to do this... thanks!

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Sep 2014
Assuming that you are using GUIDE to develop your GUI, suppose you have your callback for push button 1, tagged as push1 as
% --- Executes on button press in push1.
function push1_Callback(hObject, eventdata, handles)
% get the path to and name of file to load
[filename,path] = uigetfile('*fs.dat');
if ischar(filename)
% concatenate the filename with the path
fullpathToFile = fullfile(path,filename);
% save the full path to handles
handles.myfile = fullpathToFile;
% write the file name to edit1
set(handles.edit1,'String',filename);
% extract the number between the last underscore and fs
fileId = char(regexp(fullpathToFile,'_[0-9]+fs','match'));
% remove the underscore and fs
fileId = fileId(2:end-2);
% write the file id to edit2
set(handles.edit2,'String',fileId);
% save the user-defined GUI data
guidata(hObject,handles);
end
The above use uigetfile to allow the user to select the fs.dat file to load. The name of the file is saved to the *handles structure for future reference. The set command is used "write" the filename (and later file integer ID) to the edit text widgets. We then use regexp to match in the string for an integer (with one or more digits, hence the +) between and underscore and the 'fs' text. Finally, we save the updated handles structure with guidata.
To go the other direction, in your other callback, you will want to use the get call to grab the integer (that the user has changed) from the edit2 widget, and you can use regexprep to replace the original ID with this one
newFileId = ['_' char(get(handles.edit2,'String')) 'fs'];
handles.myfile = regexprep(handles.myfile,'_[0-9]+fs',newFileId);
guidata(hObject,handles);
Given the above, you should be able to construct your two callbacks to do what you want.
  1 Comment
aurc89
aurc89 on 2 Oct 2014
It is very clear, thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by