Help with loading in a GUI

1 view (last 30 days)
aurc89
aurc89 on 25 Sep 2014
Commented: Joseph Cheng on 29 Sep 2014
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!

Accepted Answer

Joseph Cheng
Joseph Cheng on 25 Sep 2014
Edited: Joseph Cheng on 25 Sep 2014
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 Comments
aurc89
aurc89 on 26 Sep 2014
Thank you very much! Actually the name of my file is not 'data_001' but 'VIS_measure_08_001fs', and from here I have to extract the three final numbers before 'fs', i.e. 001. How can I do ? I don't manage with your code...
Joseph Cheng
Joseph Cheng on 29 Sep 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!