I calculate the coordinates of B with point A and alpha. In a loop I want to vary the coordinates x_A and y_A in a 0.1-grid, starting at S=(x_S, y_S) and ending at T. Each A and related B should be written in an excel-sheet like this - first writing some unchanged values, then all A and B values:
x_S
y_S
alpha_1
alpha_2
x_A_1_1 x_A_1_2 ... x_A_1_m ... x_A_2_1 ... x_A_n_1 ... x_A_n_m
y_A_1_1 y_A_1_2 ... y_A_1_m ...
x_B_1_1 x_B_1_2 ... x_B_1_m ...
y_B_1_1 y_B_1_2 ... y_B_1_m ...
Here my try for the loops
for i=x_S:0.1:x_T
x_A(i)=x_S +i;
for j = y_S:0.1:y_T
y_A(j)=y_S +j;
end
A = [x_A; y_A];
B = [x_B; y_B] %calculated by trigonometric functions, where put it, inside loop or after/before?
end
So i want to save ALL A in a Matrix AM and all B in BM, so
A = [a1 a2 a3 .. a(n x m)] %in a (n x m)x2 Matrix
B = [b1 b2 .. b(n x m)]
for Saving it in the same excel sheet I want something like that:
xlRange_xS = 'A1';
xlRange_yS = 'A2';
xlRange_alpha1 = 'A3';
xlRange_alpha2 = 'A4';
xlRange_A = 'A5';
xlRange_B = 'A7';
xlswrite(filename1, x_S, sheet, xlRange_xS)
xlswrite(filename1, y_S, sheet, xlRange_yS)
...
xlswrite(filename1, AM, sheet, xlRange_A)
xlswrite(filename1, BM, sheet, xlRange_B)
I know some codes are wrong but hope you understand what the code SHOULD do. I realized it is better to first save all values in an array and then usw xlswrite for that array and not the single values. But how do I save the loop values A in a matrix and how the values for B? Can I even save them into the same array (if I cannot use xlswrite more then once). And I need the step to be 0.1, but it always shows the error
Subscript indices must either be real positive integers or logicals.
After reading a lot of questions today I cannot think clearly anymore and mix everything up. Thank you!

 Réponse acceptée

Guillaume
Guillaume le 26 Jan 2017
Don't use loops. Learn vectorised operations.
%data generation:
[x_A, y_A] = meshgrid(x_S:0.1:x_T, y_S:0.1:y_T);
A = [reshape(x_A, 1, []); reshape(y_A, 1, [])];
B = cos(A); %replace with whatever function you want
%saving to excel.
%If you want to save it all at once, you need to store it all in a cell array:
emptycols = repmat({[]}, 1, size(A, 2)-1);
xldata = [{x_S}, emptycols;
{y_S}, emptycols;
{alpha1}, emptycols;
{alpha2}, emptycols;
{[]}, emptycols;
num2cell(A);
num2cell(B)];
xlswrite(somefile, xldata)

3 commentaires

Thank you, it works perfect!!
I only had to transpose the xldata before using xlswrite because it was too many columns.
Now in my calculations I used to do this, and i hope it is still doing it right, only i have problems adding D to A'*C' now...How should i do it? This was the way I did it with only one A=[x_A; y_A]
%A = 2x1586 double
C = [1, 2; 3, 4];
D = [5 6];
V1 = [7 8];
V2 = [9 10];
H1 = 11;
H2 = 12;
R1 = A'*C1' + D; %used to be:
R2 = A'*C1' + D; % [r1 r2]=[x_A y_A]*[c1 c2; c3 c4] + [d1 d2]
p1 = R1(1,1);
q1 = R1(1,2);
p2 = R2(1,1);
q2 = R2(1,2);
J = [p1 q1; p2 q2];
G1 = A*V1 + H1; %used to be:
G2 = A*V2 + H2; % G = [a1; a2]*[v11 v12] + H1
k = [G1; -G2];
B = (J)^(-1)*k;
since A was [a1 a2] before and is now the whole matrix, i don't know how to use it for calculations since matrix dimensions must agree. At the moment it is working, if I ignore D
R1 = A'*C1' %+ D;
but of course i need to add D.
Also i am not sure if it is doing the right calculations for the rest but testing it right now.
Yes, the rotation is still the same operation, you can multiply your rotation matrix directly with the transpose of A. I'm not sure you should be transposing C, it depends what elements c1, c2, etc. refer to.
R = A.' * C; %or C.' maybe
For the translation, it looks like you're not using R2016b as what you wrote would work in that version (thanks to the new implicit expansion). For earlier versions of matlab, use bsxfun
R = bsxfun(@plus, A.' * C, D);
%in R2016b, the above is simply
%R = A.' * C + D;
NeedHelp
NeedHelp le 27 Jan 2017
Perfect! Thank you so much (I don't know why I have two profiles here, but I also am Citybird, will accept your answer at other profile later)

Connectez-vous pour commenter.

Plus de réponses (1)

NF
NF le 4 Juin 2019

0 votes

Hi,
I have a cell array with multiple matrices. when using xlswrite, only one matrix appears at the xls file.
What should I do?
attached is the code

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by