Wants to add each iteration data in the next column.

x=rch;
flow=rch(:,8);
c=0;
for i=1:166 %c=c+1
for c=c+1 %c<166
ind1 = rch(:,2) == c;
flow_out2= flow(ind1);
end
% xlswrite('out.xlsx', flow_out2','A1:FJ2191')
flow_out2 = [flow_out2 dataToAppend];
end

8 Commenti

Rik
Rik il 6 Feb 2020
Modificato: Rik il 6 Feb 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
On a basic level you would need something like the code below
A=zeros(100,10);
for n=1:size(A,2)
%do some calculation
x=n*rand(100,1);
%store to column of A
A(:,n)=x;
end
But your code is so unclear I'm not sure what edit to suggest.
x=rch;
flow=x(:,8); %this produces 2191 days of water discharge value m/s (size 2191x1)
c=0;
for i=1:5 %c=c+1
for c=c+1 %c<166
ind1 = rch(:,2) == c;
flow_out2= flow(ind1);
dataToAppend = flow_out2+i;
end
% xlswrite('out.xlsx', flow_out2','A1:FJ2191')
%flow_out3 = cat(2,flow_out2);
% flow_out2 = [flow_out2 dataToAppend];
%flow_out2 = horzcat(flow_out2, dataToAppend);
allData = [flow_out2 dataToAppend]; % I do get second iteration data stacked in next column as 2191x2 matrix. though on 3rd iteration its gets replaced by next iteration data.
end
input file link
https://my.pcloud.com/publink/show?code=XZSH1WkZdOe6RNuE5fhnXpJrf5uVwmlWG0Qk
You commented out the line that would do that for you:
flow_out2 = [flow_out2 dataToAppend];
Although I would still suggest trying to put your code in the framework I showed you.
shobhit pipil
shobhit pipil il 6 Feb 2020
Modificato: shobhit pipil il 6 Feb 2020
Hi Rik,
I tried ' flow_out2 = [flow_out2 dataToAppend]; ' , but the 'flow_out2' never expanded beyond two cloumn. I am trying to reframe the code as per your suggestions.
flow_outF=[];
x=rch;
flow=x(:,8);
c=0;
for i=1:5 %c=c+1
for c=c+1 %c<166
ind1 = rch(:,2) == c;
flow_out2= flow(ind1);
%dataToAppend = flow_out2+i;
end
% xlswrite('out.xlsx', flow_out2','A1:FJ2191')
%flow_out3 = cat(2,flow_out2);
% flow_out2 = [flow_out2 dataToAppend];
%flow_out2 = horzcat(flow_out2, dataToAppend);
flow_outF(:,c) = flow_out2;
end
% This code works for me, if I run it in Breakpoint mode. but full execution doesn’t produce anything.
how do I control for loop in the code.
for c=c+1 % is it valid
if not, how do I control this loop?
thanks
First write the contents of your loop. What is the code you want to run for a single iteration? Only then should you start with putting a loop around that.
As for your last question: try to think what you are saying with that line. You are telling Matlab to create a vector (c+1) and run the code inside the loop for each element, using c as the variable to store the elements in. While that is valid code, it is probably not what you mean.
Have you considered doing a tutorial to get up to speed with the basic Matlab syntax?

Accedi per commentare.

 Risposta accettata

rch = rch_1960_NY6_1975;
flow_outF=zeros(3652,166);
x=rch;
flow=x(:,8);
c=0;
for i=1:166 %c=c+1
for c=c+1 %c<166
ind1 = rch(:,2) == c;
flow_out2= flow(ind1);
%dataToAppend = flow_out2+i;
end
% xlswrite('out.xlsx', flow_out2','A1:FJ2191')
%flow_out3 = cat(2,flow_out2);
% flow_out2 = [flow_out2 dataToAppend];
%flow_out2 = horzcat(flow_out2, dataToAppend);
flow_outF(:,c) = flow_out2;
if c==166
end
end

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by