Help with loading in a GUI
조회 수: 1 (최근 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!