How to retrieve outputs from an .m file into my GUI?

조회 수: 1 (최근 30일)
Diogo Carvalho
Diogo Carvalho 2020년 9월 8일
편집: Diogo Carvalho 2020년 9월 11일
I'm building a GUI using guide in which I want to use a drop down menu to perform a series of procedures.
Those are as follows:
1 - Load a .txt file based on one of two possible choices;
2 - If the second option is selected, I have the gui retrieve four variables from a .txt file (dt, eEXP, wEXP and R);
3 - These variables are then used as inputs in an external function (blastLoadFun) which uses all four previously mentioned variables and has 2 outputs (Z and P);
4 - Finally, I want to keep these two outputs, for later use.
I've tried to accomplish this in the following ways:
-Assigning a random value to Z and P (ex. assignin('base','Z',0)), then trying to call the function: [Z,P] = blastLoadFun(eEXP, wEXP, R);
-Simply calling the function blastLoadFun(eEXP, wEXP, R)

채택된 답변

Diogo Carvalho
Diogo Carvalho 2020년 9월 11일
편집: Diogo Carvalho 2020년 9월 11일
I figured it out. Apparently, the input variables that went into the function still weren't considered as such. Thus, the function was unable to run. The assignin() function isn't necessary before running the function.
I'll just post the relevant part of the code:
else
[filename pathname] = uigetfile('*.txt','Explosive parameters'); %if choice 2
if ~ischar(filename) && ~ischar(pathname) % If no file
msgbox('No file selected.','Error');
set(handles.BuildingText,'String','Error: Must select a file.');
return
end
ExplosivePar = importdata([pathname filename]);
dt = ExplosivePar(1);
eEXP = ExplosivePar(2);
wEXP = ExplosivePar(3);
R = ExplosivePar(4);
[Z,P] = blastLoadFun(eEXP,wEXP,R);
assignin('base','Z',Z);
assignin('base','P',P);
end

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 9월 8일
One of the possible solutuons is to call/excute your pre-created function file, that loads your *.txt data into MATLAB (depicted in step 1) workspace, within the callback function used for your GUI.
  댓글 수: 1
Diogo Carvalho
Diogo Carvalho 2020년 9월 8일
Apologies, Sulaymon, as I wasn't clear in my submission. I have steps 1 and 2 down, the problem lies in running my function (blastLoadFun) after already having loaded variables from the .txt file.
--- Executes on selection change in popupChoice.
function popupChoice_Callback(hObject, eventdata, handles)
global dt ffunction eEXP wEXP R Z P
contents = cellstr(get(hObject,'String'));
popChoice = contents{get(hObject,'Value')};
if strcmp(popChoice,'Load blast load function') == 1 %if choice 1
[filename pathname] = uigetfile('*.txt','Blast load function');
if ~ischar(filename) && ~ischar(pathname) % If no file
msgbox('No file selected.','Error');
set(handles.BuildingText,'String','Error: Must select a file.');
return
end
LoadsFile = importdata([pathname filename]);
assignin('base','dt',LoadsFile(1));
assignin('base','ffunction',LoadsFile(2:end));
else
[filename pathname] = uigetfile('*.txt','Explosive parameters'); %if choice 2
if ~ischar(filename) && ~ischar(pathname) % If no file
msgbox('No file selected.','Error');
set(handles.BuildingText,'String','Error: Must select a file.');
return
end
ExplosivePar = importdata([pathname filename]);
assignin('base','dt',ExplosivePar(1));
assignin('base','eEXP',ExplosivePar(2));
assignin('base','wEXP',ExplosivePar(3));
assignin('base','R',ExplosivePar(4));
blastLoadFun(eEXP,wEXP,R);
end
As you can see, I already got the variables in step 2, but when I use them to run blastLoadFun, nothing really happens, as neither Z or P get saved in the workspace.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by