Help with loading data in GUI
6 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Geoff Hayes
2014-9-30
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!