How to print the string value using fprintf

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to print the string value using fprintf

GK19
Hi,

a = (aa bb cc dd)

for ion=1:1:length(a)
and some lines of code
then later i have some functionality
---------------
p =  a(ion);
Results if i use
fprintf("%10s", p);
error: fprintf: wrong type argument 'cell'

suppose if i use
disp(p)
Output
{
  [1,1] = aa
}

I want just the 'aa' as the output using fprintf only
How do i do that ? Kindly help



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: How to print the string value using fprintf

Doug Stewart-4


On Wed, Apr 22, 2020 at 4:32 PM GK19 <[hidden email]> wrote:
Hi,

a = (aa bb cc dd)

for ion=1:1:length(a)
and some lines of code
then later i have some functionality
---------------
p =  a(ion);
Results if i use
fprintf("%10s", p);

try

p{1}

 
error: fprintf: wrong type argument 'cell'

suppose if i use
disp(p)
Output
{
  [1,1] = aa
}

I want just the 'aa' as the output using fprintf only
How do i do that ? Kindly help



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: How to print the string value using fprintf

nrjank
Administrator
In reply to this post by GK19

a = (aa bb cc dd)


well, that's obviously not your first line since that throws a syntax error.

octave:1> a = (aa bb cc dd)
parse error:

  syntax error

>>> a = (aa bb cc dd) 

what is your actual code?


Reply | Threaded
Open this post in threaded view
|

Re: How to print the string value using fprintf

GK19
In reply to this post by Doug Stewart-4
Thanks! it worked

But do you have any other way of representing it ?



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: How to print the string value using fprintf

nrjank
Administrator
On Wed, Apr 22, 2020 at 5:00 PM GK19 <[hidden email]> wrote:
Thanks! it worked

But do you have any other way of representing it ?



I'm not sure what your real data looks like.  If they are all strings, then if they are the same length you could do a char array:

octave:1> a = ['abc'; 'def'; 'ghi']
a =

abc
def
ghi

octave:2> a(1)
ans = a
octave:3> a(1,:)
ans = abc
octave:4> a(3,:)
ans = ghi

if you need more flexibility, you could use a cell array:

octave:7> a = {'abc'; 'defghi'; 'jklmnop'}
a =
{
  [1,1] = abc
  [2,1] = defghi
  [3,1] = jklmnop
}

octave:8> a{1}
ans = abc
octave:9> a{2}
ans = defghi
octave:10> a{3}
ans = jklmnop

those could then be indexed in a loop.