Variables in function are not stored in workspace

14 vues (au cours des 30 derniers jours)
Sujay A
Sujay A le 5 Avr 2020
Commenté : Sujay A le 5 Avr 2020
When I run the following code, I cant access g1 and g2. How can I get g1 and g2 to be stored in the workspaceso that I can access it later?
function g = helpme(x)
g=0;
syms f;
for i=1:length(x)
if length(x)~=1
g= cumsum(x);
[c,index] = min(abs(g-sum(x)/2));
if(i<=index)
f(:,i) = zeros(1);
else
f(:,i) = ones(1);
end
g1 = x(1,1:index)
g2 = x(1,index + 1:length(x))
end
end
end
commandwindow
ss=[27 24 10 9 9 9 6 4 3 3 3 3 3];
hh=helpme(ss)

Réponse acceptée

Steven Lord
Steven Lord le 5 Avr 2020
If you want g1 and g2 to be available in the workspace in which you call helpme, specify them as output arguments for helpme:
function [g, g1, g2] = helpme(x)
AND call helpme with enough output arguments.
>> [hh, thisIsg1, thisIsg2] = helpme(ss)
You need both these steps.
  1 commentaire
Sujay A
Sujay A le 5 Avr 2020
Thanks a lot, It worked perfectly.

Connectez-vous pour commenter.

Plus de réponses (1)

David Hill
David Hill le 5 Avr 2020
Click on the line in the function where you want to place a breakpoint. Execute the function. The function will stop at the breakpoint and your variables will be in the workspace.
  1 commentaire
Sujay A
Sujay A le 5 Avr 2020
function g = helpme(x)
g=0;
syms f;
for i=1:length(x)
if length(x)~=1
g= cumsum(x);
[c,index] = min(abs(g-sum(x)/2));
if(i<=index)
f(:,i) = zeros(1);
else
f(:,i) = ones(1);
end
g1 = x(1,1:index)
g2 = x(1,index + 1:length(x))
end
end
end
For the above function, I would like to put breakpoints to access g1 and g2, can you please tell me how can I do it?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by