Matlab小问题合辑【长期稳定更新】

Q:

Suppose I have the following string:

s = 'Foo 1.000 3.000 3.554'

I would like to read it with the textscan function as follows.

[name x y z] = textscan(s, '%s %f %f %f')

However, when I do this, I always get the Too many output arguments error.

I think it has to do with the fact that textscan outputs a cell array, but I could not discover how to work around this problem and the desired effect.

A:

You'll need two lines to do what you want. First you get the desired valued into a dummy variable, then distribute the data with deal:

dummy = textscan(s, '%s %f %f %f');
[a,b,c,d] = deal(dummy {:});

Note: After Matlab 7.0, deal() is not neccesary.

Q:

i have a file called hello.txt using wordpad which contains the matrix

2 7 3

2 6 9

now i have a vector v = [1 2 3] and i want to add this vector to the hello.txt file so when i open the hello.txt file i should have

2 7 3

2 6 9

1 2 3

how can i do this?

A:

fid = fopen('hello.txt', 'at');
fprintf(fid, '%d %d %d\n', v);
fclose(fid);
 
 
原文地址:https://www.cnblogs.com/ShaneZhang/p/3092310.html