Variables not appearing in workspace

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
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
Hamed Davoudi el 21 de Oct. de 2020
Revision3 is the main script that has these lines.
I have a demand array with "I" rows and 5 columns. I want to ask the user to define how many columns my demnad array should have, and which column have to be used. So, in the whole code the only lines that are related to shape of my demand array are these lines. Before line 77, I am definig other variables and after that I use them in my optimization.

Iniciar sesión para comentar.

Respuestas (1)

drummer
drummer el 21 de Oct. de 2020
Editada: drummer el 21 de Oct. de 2020
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
Hamed Davoudi el 21 de Oct. de 2020
Editada: Hamed Davoudi el 21 de Oct. de 2020
When I writethese lines in Command Window, indx retruns a vector not a value. I need that vector for reshaping my demand array.
But when I use these lines in my code, that "indx" variable does not appear in my workspace and is not working in the code either.
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.
drummer
drummer el 21 de Oct. de 2020
Editada: drummer el 21 de Oct. de 2020
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
Hamed Davoudi el 21 de Oct. de 2020
Thanks for replying but it does not work.
Unrecognized function or variable 'tf'.
It's like these lines are not working when I use them in my code. They are Ok in command window but not in my code. The list question shows up and I can select the options, but no matter what is the selection, the code does not continue if I use either of "indx" or "tf" variables in another calculation.
drummer
drummer el 21 de Oct. de 2020
The list variable appear in your workspace?
aintakic aintakic
aintakic aintakic el 21 de Oct. de 2020
hjfghfhfgdfgdgdfgdfdgdfgh
Hamed Davoudi
Hamed Davoudi el 21 de Oct. de 2020
The "list", "indx" and "tf" do not appear, but the other variables are appearing.
Actually, now when I add a simple variable like "XX=2" it does not appear either!
Walter Roberson
Walter Roberson el 21 de Oct. de 2020
attach your complete code
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de Oct. de 2020

Comentada:

el 21 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by