Checking if a matrix is right dimensions.

6 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2014 年 3 月 24 日
コメント済み: Giuseppe 2014 年 3 月 24 日
I have this code for a user to enter coordinates. This creates then the matrix y and x.
n=10; % number of lines
a= inputdlg('enter x coordinates','test',n)
x=str2num(a{1})
b= inputdlg('enter y coordinates','test',n)
y=str2num(a{1})
I want to make it so that if there is unequal numbers of y and x cordinates a error message is displayed. Would I use if, else statements using the numel command to check if numel of x equalls y.
Thanks,

採用された回答

Youssef  Khmou
Youssef Khmou 2014 年 3 月 24 日
Try
if length(a{1})~=length(b{1})
error(' Vectors must have same length');
end

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 3 月 24 日
編集済み: Image Analyst 2014 年 3 月 24 日
Try this:
numberOfCoordinates = 10; % number of coordinates.
for k = 1 : numberOfCoordinates
editBoxPrompts{k} = sprintf('Coordinate #%d', k);
end
% celldisp(editBoxPrompts);
% Get x:
caX = inputdlg(editBoxPrompts, 'X coordinates')
x = str2double(caX)
% Get rid of nans (where they didn't enter anything).\
x = x(~isnan(x))
% Get y:
caY = inputdlg(editBoxPrompts, 'Y coordinates')
y = str2double(caY)
% Get rid of nans (where they didn't enter anything).\
y = y(~isnan(y))
% Check for same lengths.
if length(x) ~= length(y)
warningMessage = sprintf('Error: length of x (%d) does not match length of y (%d)',...
length(x), length(y));
uiwait(warndlg(warningMessage));
end
  1 件のコメント
Giuseppe
Giuseppe 2014 年 3 月 24 日
Thanks for the effort I learnt a lot from this.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by