Using the answer of inputdlg into a Matlab Expression

2 ビュー (過去 30 日間)
Alessandro
Alessandro 2013 年 4 月 30 日
編集済み: MoeIsKing 2018 年 11 月 14 日
Hello everybody,
I am trying to write some piece of code which asks for the name of a previously defined variable and uses the answer in some calculations. Everything works fine when I use 'input' to ask for that name. But I want to use the 'inputdlg' command. The problem is that 'inputdlg' returns user input in a cell array. I tried converting the cell with 'str2double' but that only works for numbers (won't work when trying to convert the name of the variable!). This is the piece of code:
Source = inputdlg('Name given to the vector containing the sample: ');
Fs = inputdlg('Sampling Frequency: '); % Sampling frequency
Fs = str2double(Fs);
Source = str2double(Source(1,1));
T = 1/Fs; % Sample time
L = length(Source); % Length of signal
t = (0:L-1)*T; % Time vector
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y=fft(Source,NFFT)/L;
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 30 日
What is the error message?
Alessandro
Alessandro 2013 年 4 月 30 日
編集済み: Alessandro 2013 年 4 月 30 日
For example, if the vector I want to select is 'A', 'Source' vector will be composed of a single entry 'A' instead of being equal to the original vector. Note this does not happen when using 'input' command instead of 'inputdlg'.

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 30 日
Use
Source = char(Source)
  2 件のコメント
Alessandro
Alessandro 2013 年 4 月 30 日
That doesn't work as well, because the so-created Source is a char-type array containing 1 entry instead of being set equal to the vector specified in the inputdlg box.
Alessandro
Alessandro 2013 年 4 月 30 日
This helped anyways. By using
Source = eval(char(Source(1,1)))
i got it to work. Not very elegant, but it's still something. Thanks!

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


MoeIsKing
MoeIsKing 2018 年 11 月 14 日
編集済み: MoeIsKing 2018 年 11 月 14 日
Source = eval(char(inputdlg())) helps
THANKS

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by