Variables not appearing in workspace

9 visualizzazioni (ultimi 30 giorni)
Hamed Davoudi
Hamed Davoudi il 21 Ott 2020
Commentato: drummer il 21 Ott 2020
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 Commenti
Cris LaPierre
Cris LaPierre il 21 Ott 2020
Modificato: Cris LaPierre il 21 Ott 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 il 21 Ott 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.

Accedi per commentare.

Risposte (1)

drummer
drummer il 21 Ott 2020
Modificato: drummer il 21 Ott 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 Commenti
Walter Roberson
Walter Roberson il 21 Ott 2020
attach your complete code
drummer
drummer il 21 Ott 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.

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by