differentiate input dialog strings into either symbols or double precision values

조회 수: 2 (최근 30일)
aldburg
aldburg 2017년 11월 19일
편집: aldburg 2017년 11월 20일
I have an input dialog box where I put values or a variable I am trying to solve for. Problem I have is how do I have Matlab differentiate between numeric inputs so I can convert them to double string values or alphabet values so I can tell MATLAB that something like sigma_x is a symbol?
  댓글 수: 2
Stephen23
Stephen23 2017년 11월 19일
편집: Stephen23 2017년 11월 19일
"double string values" is confusing, as MATLAB does not have any class "double string". Or "alphabet", for that matter. Do you want the output to be:
  • double
  • string
  • char
  • something else?
aldburg
aldburg 2017년 11월 19일
If I type in 0 in the dialog box I want the string to be converted to double but if I type x in the same box it will convert the string to a symbolic variable. Trying to make a robust input dialog box where if I input a value or a symbol it can produce a solution it for me.

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2017년 11월 19일
편집: Stephen23 2017년 11월 19일
There are several ways your could approach this, depending on what you define as "numeric" (e.g. plus/minus sign, decimal digits, exponent notation, etc). The simplest to try would be to use str2double, which returns NaN if the input cannot be converted to double:
num = str2double(str);
if isnan(num)
% str is a "symbol"
else
% num is numeric value
end
  댓글 수: 1
aldburg
aldburg 2017년 11월 20일
편집: aldburg 2017년 11월 20일
For my code below, if x isn't the unknown how can I have MATLAB change my symbol based on the userinput to solve the simple equation x+y+z+w=100:
clear
prompt = {'x','y','z','w'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'...','...','...','...'};
options.Interpreter = 'tex';
answer = inputdlg(prompt,dlg_title,num_lines,defaultans,options)
test_for_numbers=str2double(answer);
if isnan(test_for_numbers)
% str is a "symbol"
else
% num is numeric value
end
syms x
Solution = solve(x+test_for_numbers(2,1)+test_for_numbers(3,1)+test_for_numbers(4,1)== 100, x)

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by