Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MATLAB GUI: how to read and write variable from workspace

4,397 views
Skip to first unread message

Dharmesh Nadhe

unread,
Mar 30, 2011, 2:44:22 PM3/30/11
to
Hello all,
I have created a function"acc_weight" in matlab which is as follows:
[a_rms,a_w_t,a_w_f,freq] = acc_eight(a,time,inter,'W') here a_rms,a_w_t,a_w_f and freq are outputs and a,time,inter and W (string) are inputs. I am making a GUI using guide which uses this function and calculates the output values on the basis of input given. For that I have 4 edit text corresponding to 4 inputs and 4 static text corresponding to 4 output and one push button. My requirement is to access input variable a,time from workspace. input variable inter and 'W' are given by user. and all 4 output variables should be written back to workspace. But I do not know how to access variables form workspace. Using some online help I have created following code for my GUI application. a,time,inter and W are my Tags for each input. And a_rms, a_w_t, a_w_f, freq are Tags for outputs.

function calculate_Callback(hObject, eventdata, handles)
% hObject handle to calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
acc= str2num(get(handles.a,'string')); % storing value of "a" in acc using Tag
time= str2num(get(handles.time,'string'));
inter= str2num(get(handles.inter,'string'));
wfunc=(get(handles.W,'string'))% storing value of "W" in wfunc using Tag. W is string.
[acc_rms,acc_w_t,acc_w_f,frequency]=acc_weight(acc,time,inter,wfunc);
acc_rms1=num2str(acc_rms);
acc_w_t1=num2str(acc_w_t);
acc_w_f1=num2str(acc_w_f);
frequency1=num2str(frequency);
set(handles.a_rms,'string',acc_rms1);
set(handles.a_w_t,'string',acc_w_t1);
set(handles.a_w_f,'string',acc_w_f1);
set(handles.freq,'string',frequency1);

I do not have any access to workspace in the code presented above. Can someone guide me how can I access variables from workspace and store them back to workspace?
Thanks in advance.

Uwe

unread,
Mar 30, 2011, 6:52:04 PM3/30/11
to
Hi Dharmesh,

use evalin('base',a) to get variable a from workspace and assignin('base','a_rms',a_rms) to write variable a_rms to workspace.

Uwe

"Dharmesh Nadhe" <dharmes...@gmail.com> wrote in message <imvtm6$p4a$1...@fred.mathworks.com>...

Dharmesh Nadhe

unread,
Mar 31, 2011, 6:13:04 AM3/31/11
to
Hello Uwe. Many thanks for your reply. Now, I have changed my GUI design. Function which I wish to execute is same:
[a_rms,a_w_t,a_w_f,freq] = acc_eight(a,time,inter,'W') here a_rms,a_w_t,a_w_f and freq are outputs and a,time,inter and W (string) are inputs. Please note that I have a huge data which I am importing in workspace and input "a" and "time" are vectors (with around 100000 elements) which I want to select form workspace. Input "inter" has only one value and "W" is a string. These variable I am taking from user using 2 edit texts with tag "inter" and "W". Now to select variable a and time from workspace, i have a slide box in my GUI. and one button "import data" with tag "data".
First I am importing data using following code in my GUI .m file:

function update_listbox(handles)
importfile('file_name')
vars = evalin('base','who');
set(handles.data,'String',vars)

After this I have all data imported in GUI with list of name of variables in slide box. To select time and a , which means, 2 vector inputs from list I have following code:

function [var1,var2] = get_var_names(handles)
list_entries = get(handles.data,'String');
index_selected = get(handles.data,'Value');
if length(index_selected) ~= 2
errordlg('You must select two variables',...
'Incorrect Selection','modal')
else
var1 = list_entries{index_selected(1)};
var2 = list_entries{index_selected(2)};
end

Now I guess I have entire input data to work with. On the basis of this data I have one push button"calculate" with tag"compute" to execute my above mentioned "acc_weight" function. Belows the callback function in my compute button, my code is as follows:

[time,acc] = get_var_names(handles); % here I am getting selected variables in time and acc and it works fine. But assigned values are only selected workspace variable names not the values of it. Then I have my other two inputs "inter" and "W" as follows:
inter1= str2num(get(handles.inter,'string'));
wfunc=(get(handles.W,'string')); % I want to read W as a string.
Now I wish to execute my function. I tried executing it as follows:
[acc_w_t,acc_w_f,frequency]=evalin('base'['acc_weight(',acc,',',time,',',inter1,',',wfunc,')']);
But it doesn't work. I gives error saying not able to read inter1 and W. So how can I write my function " acc_weight" using "evalin" ? how can I deal with non workspace inputs such as "inter1" and "W" ?
And how can I have output variables "a_rms","a_w_t","a_w_f" and "freq" back to workspace?
Thanks in advance.

Uwe

unread,
Apr 4, 2011, 4:37:07 PM4/4/11
to
Hi Dharmesh,

if you have the variables acc, time and inter1 in the base workspace, it should work as follows:
[acc_w_t,acc_w_f,frequency]=evalin('base','acc_weight(acc,time,inter1,'''W''')');

If you want to send a variable from your GUI to the base workspace, do it like this:
assignin('base','a_rms',a_rms);

I hope this helps :-)

Uwe

"Dharmesh Nadhe" <dharmes...@gmail.com> wrote in message <in1k3g$1il$1...@fred.mathworks.com>...

0 new messages