How to retrieve datas from multiples GUIs into a function ?

3 views (last 30 days)
Hello everyone !
I have several GUI windows to collect parameters and I want to use them in functions.
The problem is, I store datas from edit text boxes into arrays like that :
typex=getappdata(hObject,'typex');
typex(end+1)=str2double(handles.typex(1).String);
handles.typex(1).String=sprintf('block %d',numel(typex)+1);
disp(typex)
setappdata(hObject,'typex',typex);
assignin('base','typex',typex);
Then, in a callback I use them in the same GUI:
xw=getappdata(handles.xw(1),'xw');
xe=getappdata(handles.xe(1),'xe');
nx=getappdata(handles.nx(1),'nx');
typex=getappdata(handles.typex(1),'typex');
%Choix du programme de rafinement a executer
if(typex==1)
n=length(nx);
for i=1:n
xwt=xw(i);
xet=xe(i);
nxt=nx(i);
create_block_uniform_X(xwt, xet, nxt)
end
That does works.
But in an other function, I use that to retrieve the datas:
X=getappdata(0,'X')
Y=getappdata(0,'Y')
Z=getappdata(0,'Z')
nx=getappdata(0,'nx')
xw=getappdata(0,'xw')
xe=getappdata(0,'xe')
ny=getappdata(0,'ny')
yw=getappdata(0,'yw')
ye=getappdata(0,'ye')
nz=getappdata(0,'nz')
zw=getappdata(0,'zw')
ze=getappdata(0,'ze')
The thing is that for X,Y,Z it does retrieve the datas (X, Y, Z are the outputs arguments from the previous fonction 'create_block_uniform') but that return empty arrays for the others... I don't understand
Z =
Columns 1 through 13
0 1.1111 2.2222 3.3333 4.4444 5.5556 6.6667 7.7778 8.8889 10.0000 0 0 0
Columns 14 through 18
0 0 0 0 0
nx =
[]
xw =
[]
xe =
[]
Could someone help me ?
Thanks !

Accepted Answer

Dennis
Dennis on 20 May 2019
The difference is where you store your values. The setappdata/getappdata needs an object to store your data, which is provided as the first argument. So when you use hObject in the callback of a pushbutton your data is with that pushbutton. If you want to retrieve your data in another callback or another function you need to use the correct handle to the object where your data is stored. This is what you did in your first example.
In your second example you use '0' as argument, where to store your data. This is the Matlab root, personally i prefer to not store data at the matlab root, because it is easy to lose track of variables. More importantly, your values nx, xw, xe have never been stored there.
How do you call your second function? If you pass handles to that function you can access your variables exactly as in your first example.
On another topic, if you have many variables you want to store and retrieve you can use one (data) structure instead of multiple variables:
data.mw=1;
data.nx=2;
data.xe=3;
data.X=4;
data.Y=5;
data.Z=6;
setappdata(hObj,'data',data) %one variable 'data' instead of 6
  2 Comments
CHAZAUD Mathilde
CHAZAUD Mathilde on 20 May 2019
Thank you for your answer !
I tried to '0' because if I wrote the line
xw=getappdata(handles.xw(1),'xw');
it returns an error
Undefined variable "handles" or class "handles.xw".
Error in create_grid2 (line 7)
xw=getappdata(handles.xw(1),'xw');
The function in which I want to use my datas is called 'create_grid2'.
The thing I really don't understand is that is the workspace, all my variables seem to be stored.
That can be usefull as I have a lot of variables, thanks !
CHAZAUD Mathilde
CHAZAUD Mathilde on 20 May 2019
I solve my problem by replacing hObject with 0:
% typex=getappdata(hObject,'typex');
% typex(end+1)=str2double(handles.typex(1).String);
% handles.typex(1).String=sprintf('block %d',numel(typex)+1);
% disp(typex)
setappdata(0,'typex',typex);
% assignin('base','typex',typex);

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!