malpractice at num2str + title

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

malpractice at num2str + title

Fumio Hakamada
I'm using MATLAB and OCTAVE.
In the following program, number is attached to title name.
But it does not work well when  num2str(n) is attached.
Will you please confirm?

-------------------------------------------------------------
% e83.m

t = 1:100;
figure
for n=1:4
    subplot(2,2,n)
    a = " deformation - force, n=" + num2str(n);  % no for OCTAVE
    y = sin(2*pi*n*t/100);
    plot(t,y);
    title(a)
end

--
----------------------------------------
Fumio HAKAMADA, P.E.



Reply | Threaded
Open this post in threaded view
|

Re: malpractice at num2str + title

Benson Muite
On 7/4/20 10:46 AM, Fumio Hakamada wrote:

> t = 1:100;
> figure
> for n=1:4
>      subplot(2,2,n)
>      a = " deformation - force, n=" + num2str(n);  % no for OCTAVE
>      y = sin(2*pi*n*t/100);
>      plot(t,y);
>      title(a)
> end
>
Try

t = 1:100;
figure
for n=1:4
     subplot(2,2,n)
     a = strcat(" deformation - force, n=", num2str(n));
     y = sin(2*pi*n*t/100);
     plot(t,y);
     title(a)
end



Reply | Threaded
Open this post in threaded view
|

Re: malpractice at num2str + title

mmuetzel
Am 04. Juli 2020 um 09:55 Uhr schrieb "Benson Muite":

> On 7/4/20 10:46 AM, Fumio Hakamada wrote:
> > t = 1:100;
> > figure
> > for n=1:4
> >      subplot(2,2,n)
> >      a = " deformation - force, n=" + num2str(n);  % no for OCTAVE
> >      y = sin(2*pi*n*t/100);
> >      plot(t,y);
> >      title(a)
> > end
> >
> Try
>
> t = 1:100;
> figure
> for n=1:4
>      subplot(2,2,n)
>      a = strcat(" deformation - force, n=", num2str(n));
>      y = sin(2*pi*n*t/100);
>      plot(t,y);
>      title(a)
> end
>

The reason why Benson's code works but Fumio's doesn't, is that double quotes produce char arrays in Octave but they produce string objects in Matlab.
See also this item on the bug tracker:
https://savannah.gnu.org/bugs/index.php?50855

At the moment, a rule of thumb might be to not use double quotes if you want to write code that should run in Octave and in Matlab.

Markus



Reply | Threaded
Open this post in threaded view
|

Re: malpractice at num2str + title

Fumio Hakamada
Dear Markus,

Thanks for your prompt reply.
My problem was cleared.
All the best.

Hakamada


2020年7月4日(土) 17:04 Markus Mützel <[hidden email]>:
Am 04. Juli 2020 um 09:55 Uhr schrieb "Benson Muite":
> On 7/4/20 10:46 AM, Fumio Hakamada wrote:
> > t = 1:100;
> > figure
> > for n=1:4
> >      subplot(2,2,n)
> >      a = " deformation - force, n=" + num2str(n);  % no for OCTAVE
> >      y = sin(2*pi*n*t/100);
> >      plot(t,y);
> >      title(a)
> > end
> >
> Try
>
> t = 1:100;
> figure
> for n=1:4
>      subplot(2,2,n)
>      a = strcat(" deformation - force, n=", num2str(n));
>      y = sin(2*pi*n*t/100);
>      plot(t,y);
>      title(a)
> end
>

The reason why Benson's code works but Fumio's doesn't, is that double quotes produce char arrays in Octave but they produce string objects in Matlab.
See also this item on the bug tracker:
https://savannah.gnu.org/bugs/index.php?50855

At the moment, a rule of thumb might be to not use double quotes if you want to write code that should run in Octave and in Matlab.

Markus



--
----------------------------------------
Fumio HAKAMADA, P.E.