Variables not appearing in workspace

11 ビュー (過去 30 日間)
Hamed Davoudi
Hamed Davoudi 2020 年 10 月 21 日
コメント済み: drummer 2020 年 10 月 21 日
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 件のコメント
Cris LaPierre
Cris LaPierre 2020 年 10 月 21 日
編集済み: Cris LaPierre 2020 年 10 月 21 日
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 2020 年 10 月 21 日
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.

サインインしてコメントする。

回答 (1 件)

drummer
drummer 2020 年 10 月 21 日
編集済み: drummer 2020 年 10 月 21 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 21 日
attach your complete code
drummer
drummer 2020 年 10 月 21 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by