Variables not appearing in workspace
Mostrar comentarios más antiguos
I want to ask the user to define which product they want to consider in the optimization. When I am using a "listdlg" in my code it's variable, "indx", is not appearing in workspace and also when I want to use the "indx" array in another calculation it's not working and says the variable is not defiend!
I tried these lines seperately and it was working properly, but when I use them in my code which is more than 500 lines it is not working.
my code lines which are related to these problem are these:
display ('Please enter PRODUCTS you would like to consider: ') ;
list = {'Meat','Dairy','Frozen','Ice Cream','Produce'};
[indx,liststring] = listdlg('ListString',list);
P=numel(indx) ;
Production = xlsread('Data.xlsx','Demand','B1:F60');
demand = Production(1:I,[indx]);
And the error is:
Unrecognized function or variable 'indx'.
Error in Revision3 (line 77)
demand = Production(1:I,[indx]);
2 comentarios
Cris LaPierre
el 21 de Oct. de 2020
Editada: Cris LaPierre
el 21 de Oct. de 2020
If you have 500 lines of code, then we're missing what happens inbetween the time you create indx and use it in line 77.
Is Revision3 a script or a function? It it's a function, then none of the variables created inside it will appear in the Workspace.
Hamed Davoudi
el 21 de Oct. de 2020
Respuestas (1)
Well, it looks like your code needs interaction with user when using listdlg.
It is obvious that a choice should be made before executing your fourth code line. Otherwise, you won't have any value for indx.
You should wait for user's choice and then walk through the rest of your code.
Also, it makes no sense using numel(indx), as indx is a value alone. It doesn't return an array or something to get the number of elements.
The second argument of listdlg says about the user clicked the ok button, or double-clicked your item in your list.
1 if clicked, 0 if clicked esc or closed the window.
So, I don't know what you want to do after the fourth line, but in order to make indx appear in your workspace, do this:
display ('Please enter PRODUCTS you would like to consider: ') ;
list = {'Meat','Dairy','Frozen','Ice Cream','Produce'};
[indx,tf] = listdlg('ListString',list);
if tf == 1
indx % It won't execute until the user click on OK. After clicking you'll get your indx.
% insert the rest of your code here.
end
If that helps, please accept the answer.
Cheers.
9 comentarios
Hamed Davoudi
el 21 de Oct. de 2020
Editada: Hamed Davoudi
el 21 de Oct. de 2020
drummer
el 21 de Oct. de 2020
oK, so you get a vector when you choose more than one product in your list. When I told you to insert the rest of your code, I meant from the fourth line on. (where you need your indx variable).
Did you do it?
display ('Please enter PRODUCTS you would like to consider: ') ;
list = {'Meat','Dairy','Frozen','Ice Cream','Produce'};
[indx,tf] = listdlg('ListString',list);
if tf == 1
indx % It won't execute until the user click on OK. After clicking you'll get your indx.
P=numel(indx) ;
Production = xlsread('Data.xlsx','Demand','B1:F60');
demand = Production(1:I,[indx]);
end
That way, you get the number of elements assigned to P and the vector in your demand.
It's a 'flat' code right?
I mean, the other variables such as list, P, Production and demand appear on your workspace? only indx is not appearing?
So I'd agree with Cris LaPierre. If this issue your're facing is within a function, none of them will appear in the workspace of your script.
Try dbstop by left-clicking the line number you perform P = numel(idx), and check where indx is.
Hamed Davoudi
el 21 de Oct. de 2020
drummer
el 21 de Oct. de 2020
The list variable appear in your workspace?
aintakic aintakic
el 21 de Oct. de 2020
hjfghfhfgdfgdgdfgdfdgdfgh
Hamed Davoudi
el 21 de Oct. de 2020
Walter Roberson
el 21 de Oct. de 2020
attach your complete code
drummer
el 21 de Oct. de 2020
list is even before you call your window with products with listdlg. So it is likely that you are trying to reach a variable within a function. Functions have different workspaces and don't share their variables among them. That might be your problem.
It would be good to follow Walter's suggestion and attach your code.
To make sure you're within a function (last try.)
% declare them as global variables (not recommended) right before deploying listdlg.
global list
global indx
global tf
display ('Please enter PRODUCTS you would like to consider: ') ;
list = {'Meat','Dairy','Frozen','Ice Cream','Produce'};
[indx,tf] = listdlg('ListString',list);
% make a section break here to check if they appear in your workspace.
% or dbstop in the listdlg line.
Categorías
Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

