Help with loading data in GUI
13 views (last 30 days)
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!
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');
% 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
% 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
% save the user-defined GUI data
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);
Given the above, you should be able to construct your two callbacks to do what you want.