Variables not appearing in workspace
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
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.
Risposte (1)
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
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.
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!