Help with loading in a GUI
2 次查看(过去 30 天)
显示 更早的评论
I have a question about an operations with a GUI in matlab: I use a push button to load a data file from a folder, and the name of this file is then visualized as string in an edit text box (es. data_001). What I want to do is: 1) show in another edit text box just the last three characters of this string (in this case 001); 2) by changing the number in this last edit text box, I want to be able to load the data file which is in the same folder as the previous one but ending with the number I changed (e.g., by loading the first one I will see '001' in the edit text box, and if I change it and write '003' the program has to load data_003, of course if it exists! ); maybe I can use another push button to do this, I don't know... Thanks!
0 个评论
采纳的回答
Joseph Cheng
2014-9-25
编辑:Joseph Cheng
2014-9-25
Since you already have a file name called lets say 'data_001.dat' and the folder path from the first file store those in handles if you're using GUIDE. with this you can access these in other functions. you can extract the file numbers like this:
%in initial load pushbutton
[handles.file_1 handles.folderpath] = uigetfile(); %if using GUIDE will save it inside the handles structure.
% file_1 = 'data_001.dat'; %just as an example to test.
pat = '_(\w*).dat';
num = regexp(file_1,pat,'tokens');
set(handles.editbox2,'string',num{:});
so now we have the numbers of the original dat file and the other edit box is populated with this. then since we saved the folderpath inside handles if we go to the editbox callback (which should run when editbox is edited then you can
%in edit box 2 callback or other pushbutton
newfilenum = get(handles.editbox2,'string');
pat = '_(\w*).dat';
handles.file_2 = regexprep(handles.file_1,pat,newfilenum)
load(fullfile(handles.folderpath,handles.file_2));
probably should go with a pushbutton as without some additional codes the editbox callback maybe run if someone clicks in and clicks out without changing anything (it's been a while so i can't remember).
2 个评论
Joseph Cheng
2014-9-29
then instead of using regexp then try strfind(filename,'_') where it'll give you the index of each '_'. From there you can figure out that you'll need to use the last index value+1 to index value+3.
更多回答(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!