Matrix formed of a string vector and a double vector

hej got some trouble setting up my matrix, which should look like this:
M =
Drehzahl 432.5665433
Speed 455.3345;
Kartoffel 655.655444
but instead, it looks like this:
M =
Drehzahl ư
Speed LJ
Kartoffel ʏ
how come?
clc
clear all
namen = ['Drehzahl ';
'Speed ';
'Kartoffel '];
no = [432.5665433;
455.3345;
655.655444];
no1 = [432.5665433;
455.3345;
655.655444];
M = [namen, no]
N = [no1, no]
pointer = fopen('rohdatenfile', 'w');
fprintf(pointer,'%s %4.2f\n',M);
fclose(pointer);
type rohdatenfile

 Accepted Answer

Because you implicitly did a cast to char() when you wrote M--
>> M = [namen, no]
>> whos M
Name Size Bytes Class Attributes
M 3x11 66 char
>> fprintf('%s %4.2f\n',namen',no);
Drehzahl Speed Kartoffel 432.57
4.553345e+02 655.66
Unfortunately, mixed strings/numeric don't work well with the vectorization of arrays for writing formatted files--each gets handled in order if associate with the proper field. You have to do sotoo--
>> for i=1:size(namen,1),fprintf('%s %4.2f\n',namen(i,:),no(i)),end
Drehzahl 432.57
Speed 455.33
Kartoffel 655.66
>>

Categories

Asked:

on 13 Apr 2014

Answered:

on 13 Apr 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!