MATLAB: Help with loading in a GUI

guimatlab guistringstrings

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!

Best Answer

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).