Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Test if vectors are the same size

Subject: Test if vectors are the same size

From: Maxx Chatsko

Date: 19 Jul, 2010 15:28:04

Message: 1 of 13

Hello all,
    My program extracts filenames from an Excel database. Problem is, when people type the wrong filename into the Excel sheet the program returns the error that the vectors were not the same length. That is because the incorrect filename, in this case an image, does not exist. Since the image doesn't exist its data cannot be read with imread, etc, etc, etc. The error returns the line in the code where it plots the image data WL and master, which obviously won't work if one array is empty.
   In an attempt to increase user friendliness and get rid of that awful red text in the command window I would like to display an error telling the user to not be such a dumbass and check their spelling when entering filenames into the database and be thankful that such a database exists.
    How can I compare two arrays, WL and master, to make sure they are the same size?
        Thanks
        Maxx

Subject: Test if vectors are the same size

From: us

Date: 19 Jul, 2010 15:35:04

Message: 2 of 13

"Maxx Chatsko" <chatskom@chemimage.com> wrote in message <i21qu4$5m7$1@fred.mathworks.com>...
> Hello all,
> My program extracts filenames from an Excel database. Problem is, when people type the wrong filename into the Excel sheet the program returns the error that the vectors were not the same length. That is because the incorrect filename, in this case an image, does not exist. Since the image doesn't exist its data cannot be read with imread, etc, etc, etc. The error returns the line in the code where it plots the image data WL and master, which obviously won't work if one array is empty.
> In an attempt to increase user friendliness and get rid of that awful red text in the command window I would like to display an error telling the user to not be such a dumbass and check their spelling when entering filenames into the database and be thankful that such a database exists.
> How can I compare two arrays, WL and master, to make sure they are the same size?
> Thanks
> Maxx

a hint:

     help isequal; % <- and sibling...

us

Subject: Test if vectors are the same size

From: Sean

Date: 19 Jul, 2010 15:37:04

Message: 3 of 13

"Maxx Chatsko" <chatskom@chemimage.com> wrote in message <i21qu4$5m7$1@fred.mathworks.com>...
> Hello all,
> My program extracts filenames from an Excel database. Problem is, when people type the wrong filename into the Excel sheet the program returns the error that the vectors were not the same length. That is because the incorrect filename, in this case an image, does not exist. Since the image doesn't exist its data cannot be read with imread, etc, etc, etc. The error returns the line in the code where it plots the image data WL and master, which obviously won't work if one array is empty.
> In an attempt to increase user friendliness and get rid of that awful red text in the command window I would like to display an error telling the user to not be such a dumbass and check their spelling when entering filenames into the database and be thankful that such a database exists.
> How can I compare two arrays, WL and master, to make sure they are the same size?
> Thanks
> Maxx

assert(all(size(WL)==size(master)),'Don''t be such a dumbass!!');

Subject: Test if vectors are the same size

From: Andy

Date: 19 Jul, 2010 15:45:04

Message: 4 of 13

"Maxx Chatsko" <chatskom@chemimage.com> wrote in message <i21qu4$5m7$1@fred.mathworks.com>...
> Hello all,
> My program extracts filenames from an Excel database. Problem is, when people type the wrong filename into the Excel sheet the program returns the error that the vectors were not the same length. That is because the incorrect filename, in this case an image, does not exist. Since the image doesn't exist its data cannot be read with imread, etc, etc, etc. The error returns the line in the code where it plots the image data WL and master, which obviously won't work if one array is empty.
> In an attempt to increase user friendliness and get rid of that awful red text in the command window I would like to display an error telling the user to not be such a dumbass and check their spelling when entering filenames into the database and be thankful that such a database exists.
> How can I compare two arrays, WL and master, to make sure they are the same size?
> Thanks
> Maxx

It seems like an awfully clunky approach to have your user type file names exactly into an Excel file and then try to match those against a database. Is there any reason you're not using uigetfile or some similar approach?

Subject: Test if vectors are the same size

From: Walter Roberson

Date: 19 Jul, 2010 15:47:08

Message: 5 of 13

Sean wrote:

> assert(all(size(WL)==size(master)),'Don''t be such a dumbass!!');

Sean, what would happen if the two had a different number of dimensions?

Subject: Test if vectors are the same size

From: Sean

Date: 19 Jul, 2010 15:55:15

Message: 6 of 13

Walter Roberson <roberson@hushmail.com> wrote in message <0s_0o.18055$0A5.13342@newsfe22.iad>...
> Sean wrote:
>
> > assert(all(size(WL)==size(master)),'Don''t be such a dumbass!!');
>
> Sean, what would happen if the two had a different number of dimensions?

It would fail :(
It could easily be remedied with another assertion:
assert(all(size(size(wl))==size(size(master))),'Your selections don''t have the same number dimensions!!'); %Note a more polite and helpful comment. Though rude comments are the best way to get a laugh out of your coworkers!

Subject: Test if vectors are the same size

From: Sean

Date: 19 Jul, 2010 16:05:08

Message: 7 of 13

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i21sh3$i8v$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <0s_0o.18055$0A5.13342@newsfe22.iad>...
> > Sean wrote:
> >
> > > assert(all(size(WL)==size(master)),'Don''t be such a dumbass!!');
> >
> > Sean, what would happen if the two had a different number of dimensions?
>
> It would fail :(
> It could easily be remedied with another assertion:
> assert(all(size(size(wl))==size(size(master))),'Your selections don''t have the same number dimensions!!'); %Note a more polite and helpful comment. Though rude comments are the best way to get a laugh out of your coworkers!

You could one-line it with:
assert(isequal(size(wl),size(master)),'The sizes are not the same');
Though it's probably more helpful for error tracking purposes to have two with more descriptive messages.

Subject: Test if vectors are the same size

From: Maxx Chatsko

Date: 19 Jul, 2010 16:07:04

Message: 8 of 13

"Andy " <myfakeemailaddress@gmail.com>
> It seems like an awfully clunky approach to have your user type file names exactly into an Excel file and then try to match those against a database. Is there any reason you're not using uigetfile or some similar approach?

     The images are opened based upon the user selection in a listbox. Since multiple images are opened (up to 15) and have their spectral data averaged and displayed on the same plot, isn't it the best way???
        Maxx

   And thanks for the help Sean, Walter, and us. I did not use the assert function, but it lead to the right answer. Unfortunately, I did tone down my message. Which leads me to another syntax error...how can I incorporate sprintf into the error dialog multiple times? I'm working on it now - just need sprintf with ~tab~ to work on the two lines shown...
  if rows==0;
      errordlg(sprintf{'No %s data on file. Troubleshoot:',tab} char(10)...
                       {' 1) Update PSL Master Sheet with latest data' char(10)...
                       ' 2) Check spelling of filenames in PSL Master Sheet ' char(10)...
                       }sprintf{'Please uncheck %s box to continue.',tab}),'FCI Data Error');
      return

Subject: Test if vectors are the same size

From: Walter Roberson

Date: 19 Jul, 2010 16:14:10

Message: 9 of 13

Maxx Chatsko wrote:
> Which leads me to another syntax error...how can I
> incorporate sprintf into the error dialog multiple times? I'm working on
> it now - just need sprintf with ~tab~ to work on the two lines shown...
> if rows==0;
> errordlg(sprintf{'No %s data on file. Troubleshoot:',tab}
> char(10)... {' 1) Update PSL Master Sheet with
> latest data' char(10)... ' 2) Check spelling of
> filenames in PSL Master Sheet ' char(10)...
> }sprintf{'Please uncheck %s box to
> continue.',tab}),'FCI Data Error');
> return

You can represent the tab character in an sprintf format by the
character pair \t

Each sprintf call needs () around its arguments. You can include several
lines of error dialog by using {} around the list.

errordlg({sprintf('No %s data on file. Troubleshoot:', tab) ...
           sprintf('\t1) Update PSL Master Sheet)' });


I am not certain, though, that \t will produce more than a small bit of
empty space for errordlg() purposes.

Subject: Test if vectors are the same size

From: Maxx Chatsko

Date: 19 Jul, 2010 16:23:04

Message: 10 of 13

Walter Roberson <roberson@hushmail.com> return
> You can represent the tab character in an sprintf format by the
> character pair \t
>
> Each sprintf call needs () around its arguments. You can include several
> lines of error dialog by using {} around the list.
>
> errordlg({sprintf('No %s data on file. Troubleshoot:', tab) ...
> sprintf('\t1) Update PSL Master Sheet)' });
>
>
> I am not certain, though, that \t will produce more than a small bit of
> empty space for errordlg() purposes.

     I'm sorry, tab is a variable that represents an acquisition type (fci, rci, or swir). The problem is with char(10). It doesn't seem to like being on either side of (),{}...

Subject: Test if vectors are the same size

From: Walter Roberson

Date: 19 Jul, 2010 16:35:51

Message: 11 of 13

Maxx Chatsko wrote:
> Walter Roberson <roberson@hushmail.com> return
>> You can represent the tab character in an sprintf format by the
>> character pair \t
>>
>> Each sprintf call needs () around its arguments. You can include
>> several lines of error dialog by using {} around the list.
>>
>> errordlg({sprintf('No %s data on file. Troubleshoot:', tab) ...
>> sprintf('\t1) Update PSL Master Sheet)' });
>>
>>
>> I am not certain, though, that \t will produce more than a small bit
>> of empty space for errordlg() purposes.
>
> I'm sorry, tab is a variable that represents an acquisition type
> (fci, rci, or swir). The problem is with char(10). It doesn't seem to
> like being on either side of (),{}...

char(10) is the ascii newline character. If you are trying to break up
the lines to be presented, then just do as I did above, construct a cell
array of strings. Just remove the \t from my mini-example and run that
as as test (after defining "tab" of course.)

Subject: Test if vectors are the same size

From: Andy

Date: 19 Jul, 2010 16:48:12

Message: 12 of 13

> The images are opened based upon the user selection in a listbox. Since multiple images are opened (up to 15) and have their spectral data averaged and displayed on the same plot, isn't it the best way???
> Maxx

I seem to be misunderstanding something. Where does Excel come in? Where are the users incorrectly typing in file names?

As for this:

      errordlg(sprintf{'No %s data on file. Troubleshoot:',tab} char(10)...
                       {' 1) Update PSL Master Sheet with latest data' char(10)...
                       ' 2) Check spelling of filenames in PSL Master Sheet ' char(10)...
                       }sprintf{'Please uncheck %s box to continue.',tab}),'FCI Data Error');

There are many syntax errors here. sprintf is a function, so a call to it looks like sprintf(args), not sprintf{args}. errordlg takes a string or cell array of strings, whereas you seemed to concatenate a string with a cell array. Also, using a cell array, you don't need explicit char(10) to get line breaks. It should look something like the following:

errordlg({ sprintf('No %s data on file. Troubleshoot:', tab) ;
    ' 1) Update PSL Master Sheet with latest data';
    ' 2) Check spelling of file names in PSL Master Sheet';
    sprintf('Please uncheck %s box to continue.', tab)});
    

Subject: Test if vectors are the same size

From: Maxx Chatsko

Date: 19 Jul, 2010 17:01:22

Message: 13 of 13

Walter Roberson <roberson@hushmail.com>
> char(10) is the ascii newline character. If you are trying to break up
> the lines to be presented, then just do as I did above, construct a cell
> array of strings. Just remove the \t from my mini-example and run that
> as as test (after defining "tab" of course.)

    Right char(10) gets a new line and was in the code before I got to it. Apparently I don't need it. Thanks Walter and Andy.
                           Maxx

   PS xlsread works fine and has nothing to do with the problem

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us