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:
Store strings in matrix, write these to text file?

Subject: Store strings in matrix, write these to text file?

From: Kurt

Date: 1 Oct, 2010 12:57:07

Message: 1 of 6

Is it possible to store strings in a matrix, later retrieve them and write them to a text file using fprint()?

Currently I have the following (short summary):

for i = 1:aircraft
     for r = 1:routes
        iname = sprintf('%d',i);
        rname = sprintf('%d',r);
        theta = [' t' iname '_' rname];
        theta_vars{R} = theta;
     end
end

for r = 1:routes
     theta = theta_vars(r);
     fprintf(lp_inputs, theta);
end

But this gives the following error:
??? Error using ==> fprintf
Invalid format.

Error in ==> lp_input3 at 128
                fprintf(lp_inputs, theta);

Thanks

Subject: Store strings in matrix, write these to text file?

From: dpb

Date: 1 Oct, 2010 13:31:33

Message: 2 of 6

Kurt wrote:
> Is it possible to store strings in a matrix, later retrieve them and
> write them to a text file using fprint()?
...

> for r = 1:routes
> theta = theta_vars(r);
> fprintf(lp_inputs, theta);
> end
>
> But this gives the following error:
> ??? Error using ==> fprintf
> Invalid format.
...

doc fprintf

Is lp_inputs a valid fid?

Where's the format string? '%s' or similar...

--

Subject: Store strings in matrix, write these to text file?

From: Kurt

Date: 1 Oct, 2010 14:30:47

Message: 3 of 6

dpb <none@non.net> wrote in message <i84o2b$9na$1@news.eternal-september.org>...
> Kurt wrote:
> > Is it possible to store strings in a matrix, later retrieve them and
> > write them to a text file using fprint()?
> ...
>
> > for r = 1:routes
> > theta = theta_vars(r);
> > fprintf(lp_inputs, theta);
> > end
> >
> > But this gives the following error:
> > ??? Error using ==> fprintf
> > Invalid format.
> ...
>
> doc fprintf
>
> Is lp_inputs a valid fid?
>
> Where's the format string? '%s' or similar...
>
> --

lp_inputs is a valid fid. And the theta_vars value comes from:
        theta_vars = cell(1,200000);
        iname = sprintf('%d',i);
        rname = sprintf('%d',r);
        theta = ['t' iname '_' rname];
        theta_vars{R} = theta

Thanks

Subject: Store strings in matrix, write these to text file?

From: Walter Roberson

Date: 1 Oct, 2010 14:38:45

Message: 4 of 6

On 01/10/10 7:57 AM, Kurt wrote:
> Is it possible to store strings in a matrix, later retrieve them and
> write them to a text file using fprint()?
>
> Currently I have the following (short summary):
> for i = 1:aircraft
> for r = 1:routes
> iname = sprintf('%d',i);
> rname = sprintf('%d',r);
> theta = [' t' iname '_' rname];
> theta_vars{R} = theta;

This tells us that theta_vars will be a cell array

> end
> end
>
> for r = 1:routes
> theta = theta_vars(r);

When you access a cell array using array notation, the result you get
back is a cell array. To retrieve the content of the cell,

theta = theta_vars{r};

> fprintf(lp_inputs, theta);

Because of the cell array bit, you are going to be sending a cell array
as the argument to fprintf() which is not going to work.

After correcting for that, the behavior will depend upon whether
lp_inputs is numeric or a string. If it is a string then fprintf() will
assume a fid of 1 (standard output, usually the command window), and
would then use lp_inputs as the format, with theta as a value to be
potentially printed out. If, though, lp_inputs is numeric, then
fprintf() will use that as the fid, and then will want to treat theta as
the format, which will probably work except that you would not end up
with any line terminators or space between the various theta.

There is an exception to the above about fprintf(), and that is if
lp_inputs is a device object, then fprintf() will notice that only one
argument is given after that, and will assume a format of "%s\n"

> end
>
> But this gives the following error:
> ??? Error using ==> fprintf
> Invalid format.
>
> Error in ==> lp_input3 at 128
> fprintf(lp_inputs, theta);
>
> Thanks

Subject: Store strings in matrix, write these to text file?

From: Kurt

Date: 1 Oct, 2010 14:57:04

Message: 5 of 6

Walter Roberson <roberson@hushmail.com> wrote in message <Vnmpo.20930$wl2.10016@newsfe09.iad>...
> On 01/10/10 7:57 AM, Kurt wrote:
> > Is it possible to store strings in a matrix, later retrieve them and
> > write them to a text file using fprint()?
> >
> > Currently I have the following (short summary):
> > for i = 1:aircraft
> > for r = 1:routes
> > iname = sprintf('%d',i);
> > rname = sprintf('%d',r);
> > theta = [' t' iname '_' rname];
> > theta_vars{R} = theta;
>
> This tells us that theta_vars will be a cell array
>
> > end
> > end
> >
> > for r = 1:routes
> > theta = theta_vars(r);
>
> When you access a cell array using array notation, the result you get
> back is a cell array. To retrieve the content of the cell,
>
> theta = theta_vars{r};
>
> > fprintf(lp_inputs, theta);
>
> Because of the cell array bit, you are going to be sending a cell array
> as the argument to fprintf() which is not going to work.
>
> After correcting for that, the behavior will depend upon whether
> lp_inputs is numeric or a string. If it is a string then fprintf() will
> assume a fid of 1 (standard output, usually the command window), and
> would then use lp_inputs as the format, with theta as a value to be
> potentially printed out. If, though, lp_inputs is numeric, then
> fprintf() will use that as the fid, and then will want to treat theta as
> the format, which will probably work except that you would not end up
> with any line terminators or space between the various theta.
>
> There is an exception to the above about fprintf(), and that is if
> lp_inputs is a device object, then fprintf() will notice that only one
> argument is given after that, and will assume a format of "%s\n"
>
> > end
> >
> > But this gives the following error:
> > ??? Error using ==> fprintf
> > Invalid format.
> >
> > Error in ==> lp_input3 at 128
> > fprintf(lp_inputs, theta);
> >
> > Thanks

Is it then possible to store a string like t1_1 into a matrix not using a cell array?

Subject: Store strings in matrix, write these to text file?

From: Walter Roberson

Date: 1 Oct, 2010 16:14:32

Message: 6 of 6

On 01/10/10 9:57 AM, Kurt wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <Vnmpo.20930$wl2.10016@newsfe09.iad>...
>> On 01/10/10 7:57 AM, Kurt wrote:
>> > Is it possible to store strings in a matrix, later retrieve them and
>> > write them to a text file using fprint()?
>> >
>> > Currently I have the following (short summary):
>> > for i = 1:aircraft
>> > for r = 1:routes
>> > iname = sprintf('%d',i);
>> > rname = sprintf('%d',r);
>> > theta = [' t' iname '_' rname];
>> > theta_vars{R} = theta;

> Is it then possible to store a string like t1_1 into a matrix not using
> a cell array?

Yes. However, because your "i" and "r" might assume values greater than
9, the length of the string might vary, but all strings stored in a
(non-cell) array must be the same length.

strmaxlen = ceil(log10(aircraft)) + ceil(log10(routes)) + 2;
theta_vars = char(aircraft*routes, strmaxlen);
counter = 0;
for i = 1:aircraft
   for r = 1:routes
     this_theta = sprintf('t%d_%d', i, r);
     counter = counter + 1;
     theta_vars(counter,1:length(this_theta)) = this_theta;
   end
end


After this, theta_vars would be an array of strings, each row
blank-padded if necessary.


Personally, I wouldn't bother with any of this. For example:

for i = 1:aircraft
   this_format = sprintf('t%d_%%d\\n', i);
   fprintf(lp_inputs, this_format, 1:routes);
end

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