フィルターのクリア

help with concatenation to open file

2 ビュー (過去 30 日間)
lucas
lucas 2013 年 10 月 23 日
コメント済み: Vivek Selvam 2013 年 11 月 5 日
hello, can anyone help me to make this concatenation work?
I need a routine that ask the user to type the word 'work' and number '2' and also, only permits receive the answer 'work' and '2'
i tought something like this  
% enter the right name of the file
a = {'Type the name of the file:'}; % u need to type work
work = inputdlg (a);
t=str2num(char(work));
_if t =~ work
..._
% enter the right number of the file
b = {'Type the number of the file:'}; % you need to type 2
number = inputdlg (b);
n=str2num(char(number(1)));
if n~=2
disp('ERROR: Please type 2');
else
% concatenate
name = [ t, '_' , n ];
name_file = [ name, '.txt' ];
load (name_file);
thx :)
  1 件のコメント
dpb
dpb 2013 年 10 月 23 日
Well, if you're only going to allow one answer, why make the user work (so to speak)? Just define it.
But, the general question -- to compare a string
doc strncmp % and friends
To compare numeric, just use '==' or for arrays see
doc isequal
As a stylistic note, dereference the cell array contents with the curly braces "{}" instead of using char()
n=str2num(num{1});
This kind of logic is also a good location for a while...end loop enclosing a try...catch block to handle error and repeat the question until proper answer is obtained (or user gives up in frustration--don't forget to have a way to exit)

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

採用された回答

Vivek Selvam
Vivek Selvam 2013 年 10 月 23 日
編集済み: Vivek Selvam 2013 年 10 月 23 日
Try doc function for the new functions you find in the following code.
This is one way to do what you want:
a = 'Type the name of the file:'; % u need to type work
b = 'Type the number of the file:'; % you need to type 2
t = ''; % initialize file name
n = NaN; % initialize file number
tRequired = 'work';
nRequired = 2;
% enter the right name of the file
while strcmp (t,tRequired) == 0 % loop till the file name is not same
t = input (a,'s'); % get input as string
end
% enter the right number of the file
while n ~= nRequired % loop till the number of file is not same
n = input (b,'s'); % get input as string
n = str2double(n);
end
name = [ t, '_' , num2str(n) ];
name_file = [ name, '.txt' ];
disp(name_file);
load (name_file);

その他の回答 (1 件)

lucas
lucas 2013 年 10 月 25 日
ty vivek!:)
  1 件のコメント
Vivek Selvam
Vivek Selvam 2013 年 11 月 5 日
You are welcome, Lucas.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by