Update values in a vector

2 views (last 30 days)
Nu9
Nu9 on 20 Oct 2011
hi, i want to update values in a vector but i don't know why, i'v this:
a=[5*10^(-3),0,1.6,10,-1.5,1*10^(-5),-1*10^(-3),1*10^(-6)]
b=[5*10^(-2),0.01,5,25,0.5,1*10^(-3),-1*10^(-5),-1*10^(-5)]
save('var_inic.mat','a')
evalin('base','load(''var_inic.mat'')');
load ('var_inic')
my script need to use those values in a and b even though the user don't input any value.if the user want to input values it has a editbox that saves the value:
function [] = E42_call(src,evendata)
% Callback for secondary GUI editbox.
S = get(0,'userdata');
str20=get(src,'string');
if (isempty(str2num(str20)))
set(src,'String','0')
errordlg('Por favor só numeros','Erro','modal')
else
e42 = str2num(['single(',str20,')']);
save e42
set(S.ed5max,'string',get(gcbo,'string')) % atribui a string inserida na editbox no main
end
i want to update the any value in a or b with the value of e42, same with other variables

Answers (1)

Jan
Jan on 20 Oct 2011
The method to poof variables into the base workspace using EVALIN is prone to errors. Using a and b inside your script without defining them as inputs might seem to be an easy way, but as you see, it causes problems such that posting them in a forum is required.
Modifying the value of variables without a direct interaction will impede the debugging of your program dramatically. I strongly recommend to avoid such magic programming technique.
Some other remarks:
  • "5*10^(-3)" consumes processing time, but replies the same value as "5e-3".
  • Another magic programming example: str2num(['single(',str20,')'])|This is safer and faster: |single(str2double(str20))
  • Storing data by set(0, 'UserData') is like a global variable without a name. If you run another instance of the GUI or use this technique in different GUIs, unexpected and unpredictable results will appear. It would be much safer to store the values in the GUIs UserData, or by setappdata in the root object with a name.
  1 Comment
Nu9
Nu9 on 21 Oct 2011
you're right, i've changed my script but i really don't know how to update any position in a vector. Now it updates all the 8 variables, but if i just want to change 1 of them it puts the rest equal to zero

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!