Display variable that user requests

I have a program that controls a robot and various systems that runs for hours or days on end. I want a way for the user to select a variable to display to screen through input. It would look something like this...
% Existing variables in code would look something like this
var1 = 10;
var2 = 3;
% The program asks user if they want to display a variable
x = input('Enter variable value to display: ','s')
%User inputs 'var2'
disp(x)
%Matlab displays the value of var2, which is 3
Right now, of course, it would display the string that was input, not the variable that the user selected with their string input. I feel like I want a function like str2var instead of str2num...
I could do a big switch statement for the most common variables that the user might want to select, but I was hoping there was a better way to do this. Ive tried searching the web, but couldn't come up with anything. Any advice would be very appreciated.

 採用された回答

Walter Roberson
Walter Roberson 2021 年 9 月 22 日

0 投票

Perhaps just give them the Workspace Browser? https://www.mathworks.com/help/matlab/ref/workspace.html
You can find the names of all variables in the workspace using who or whos .
%set up for demo
aafdadf = 34213;
JO324adf = 3248.;
k_df9M = -121324;
varnames = setdiff( who(), 'varnames');
chosenvar = varnames{randi(numel(varnames))}
chosenvar = 'k_df9M'
H = str2func("@()evalin('caller', 'disp(" + chosenvar + ")');")
H = function_handle with value:
@()evalin('caller','disp(k_df9M)')
H()
-121324

3 件のコメント

Kelsey
Kelsey 2021 年 9 月 22 日
Walter, thank you so much for your response. It certainly does what I want. I am not very familiar with using function handles, so while I was looking deeper into your response, I ran into the eval function, which I believe is exactly what I was looking for. I updated my code to be:
% Existing variables in code would look something like this
var1 = 10;
var2 = 3;
% The program asks user if they want to display a variable
x = input('Enter variable value to display: ','s');
%User inputs 'var2'
eval(sprintf('disp(%s)',x))
%Matlab displays the value of var2, which is 3
If the user can be trusted to not be mailicious, is there a reason why I couldn't use this simpler implementation? Or does your answer do additional things behind the scenes that I don't understand.
(As an aside, we can assume the user knows the name of the variable they want to display, so that isn't a problem. I will implement a check to make sure their selection is a valid variable name.)
Walter Roberson
Walter Roberson 2021 年 9 月 22 日
If the user can be trusted to not be mailicious
A) They can't be trusted not to be malicious;
B) typing mistakes
Give them a menu to select from -- a drop-down box. And you can openvar() by name
x = 'var1';
openvar(x)
Kelsey
Kelsey 2021 年 9 月 22 日
Thanks for your help with this!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 9 月 22 日

コメント済み:

2021 年 9 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by