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

1 view (last 30 days)
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)

Accepted Answer

Diogo Carvalho
Diogo Carvalho on 11 Sep 2020
Edited: Diogo Carvalho on 11 Sep 2020
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

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 8 Sep 2020
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 Comment
Diogo Carvalho
Diogo Carvalho on 8 Sep 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!