3

I am familiar with python, but this is only my second day using matlab.

I understand that

a = {1 2 3}
fprintf('%i %i %i', a{1:3})

yields

1 2 3

But how come

a = {1 2 3}
fprintf('%i %i %i', a)

gives an error?

I would really like for

fprintf('%i %i %i', {1 2 3}{1:3})

to yield

1 2 3

In other words, if I have a cell array, how do I print each element with fprintf without assigning the cell array to a variable?

Any advice is appreciated.

EDIT: To elaborate: I have a challenge set to receive user input to get the name of an experiment, current date, and end date (of the experiment) and then output all of this information as well as the number of days left until the experiment concludes. I want to do this with only one line of code. My code is as follows.

fprintf(strcat(...
'\nTest: %s',...
'\nCurrent Date: %s',...
'\nEnd Date: %s',...
'\nNumber of days until completion: %i\n'...
),input('\nTest name?\n','s'),...
feval(@(dates){dates{1},dates{2},diff(datenum(dates,'mm-dd-yyyy'))},...
{input('\nCurrent Date? (mm-dd-yyyy)\n','s')...
input('\nEnd Date? (mm-dd-yyyy)\n','s')}));    

When this code is run I receive an error that essentially boils down to the problem described above, but I wanted to stick to basic examples. This should clear up why I don't want to just use a variable defined previously - there is no previously defined variable.

vim
  • 247
  • 2
  • 8
  • In the example of what you would like to do, obviously it's easier to just write fprintf('%i %i %i', 1, 2, 3). Could you clarify what purpose this might serve? – Matt Sep 17 '13 at 16:35
  • `fprintf` doesn't accept cell arrays. Why do you want to force the use of a cell array if don't have it already stored in a variable? – Eitan T Sep 17 '13 at 16:36
  • Regarding your edit: why do you want to do everything in a single statement? – Eitan T Sep 17 '13 at 16:49
  • The 'Engineering Fundamentals' course I am in introduces students to programming with Matlab. I've been learning programming for a couple of years and would like to convert my knowledge of programming in other languages to matlab, since I'm going to have to use it anyway. The assignment does not specify it needs to be one statement, I just assumed I'd learn more about the language if I set that constraint myself. – vim Sep 17 '13 at 16:55
  • 2
    Well, you've actually stumbled on a known weakness in the language. While you can sometimes produce really elegant one-liners in MATLAB, in this case achieving what you want is possible but VERY awkward (see [here](http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it)). I recommend focusing on making your code work correctly and then worry about brevity. – Eitan T Sep 17 '13 at 19:19
  • I looked over the post you referenced. I have found that I am better off just making a temporary variable. However, I still don't know if it's possible to do this in one line. Notice in the case you supplied, OP wanted something that turns out to be just one integer. I just get another cell array, which `fprintf` still can't deal with. – vim Sep 17 '13 at 21:04
  • @vim you can use subsref to expand the cell array into a comma-separated list, just like a{:}, e.g: subsref({1 2 3}, substruct('{}', {':'})). Possible, but ugly as hell. See my answer. – Eitan T Sep 23 '13 at 12:04

1 Answers1

1

A cell array is just another data type, so when you enter

a = {1 2 3}
fprintf('%i %i %i', a)

the fprintf function just sees the single variable 'a' and knows that a cell array is not compatible with fprintf.

When you index into a cell array using curly brackets {}, however, matlab returns a "comma-separated list," which can be input directly into functions as if you were writing out those values manually. So

fprintf('%i %i %i', a{1:3})

is interpreted as

fprintf('%i %i %i', 1, 2, 3)

For your second question, as you've found Matlab generally doesn't allow you to chain indexing operations onto other operations. So I think you do have to assign the cell array to a variable first.

Matt
  • 2,846
  • 2
  • 17
  • 12
  • Maybe it's just my python background speaking, but there has to be a way to achieve the same effect of chaining index operations onto other operations. When learning python, the statement "Everything is an object!" comes up over and over. I understand that matlab is not an OOP language - but that doesn't mean I can't achieve similar functionality (I hope). I just don't know how. – vim Sep 17 '13 at 16:56