How would i run different events from the same .txt file?

I run a program that uses the following information of a .txt file

4 0 0
1 1 1
3 1 1
2 3 1
3 2 1

in which the first number is the number of stations and than the coordinates x,y,z for these four stations. Now i want to expand the program to run for more events in which i have like

4 0 0
1 1 1
3 1 1
2 3 1
3 2 1
3 0 0
1 3 1
2 3 3
3 1 2
.
.
.
n 0 0 
xn yn zn

till 'n' events, being this all separate events. Is there a way to read it as different blocks but with one program? P.S. the zeros after the number of stations is because i use the command 'load' ideally i wouldn't write these zeros. P.S.2 the number of stations and the coordinates don't necessarily need to be in the same .txt file

6 Comments

Is there a definite number of x, y, z coordinate measurements after a single station reading? If so, you could import the whole matrix then use reshape...
What does the raw .txt file data look like?
The number of x,y,z coordinate measures are determined by the number of stations, so if it is a 4 such as above there are going to be 4 coordinate readings
Is this program independent of MATLAB? I know that you can execute programs and use inputs, but if the program is not in MATLAB itself then the ability to complete multiple runs within a single session of the program is something that needs to be built into the program itself. It is possible to use a loop to run the program multiple times in MATLAB, but that's a bit different.
Reading a text file and splitting it into whatever you want is easy. While I understood very well how you want the file to be decoded, I did not understand at all how you want that data to be stored in memory.
The easiest would be to use a cell array where each cell is one of these event, e.g. for your example:
events = {[1 1 1; 3 1 1; 2 3 1; 3 2 1], [1 3 1; 2 3 3; 3 1 2]}
a 1x2 cell array (2 events) where the 1st cell (1st event) is a 4x3 matrix (4 stations) and the 2nd cell (2nd event) is a 3x3 matrix (3 stations).
Is that what you want?
Bob Nbob, no, i use only matlab. That is what i want to do actually, run the program again and again but using different data that is contained in this .txt file and in such a way that it only runs these kind of blocks at a time
Guillaume, i think that is it, i want to have multiple arrays, so that i can run them through the code individually.

Sign in to comment.

 Accepted Answer

njj1
njj1 on 19 Apr 2018
Edited: njj1 on 19 Apr 2018

You should import the entire matrix and then begin the splitting process.

A = dlmread('Nest.txt',' '); %this assumes there are no headers or columns that you don't want
k = 1;
while 1
  n = A(1,1); %this is presumably the number of rows that you want to take after the first line
  X{k} = A(2:n+1,:);
  if size(A,1)<n+2
    break
  end
  A = A(n+2:end,:);
  k = k+1;
end

Hopefully this works for you.

8 Comments

Thanks! one thing though, my code relies on getting the matrix of each station, so for these two stations i would have a 4x3 and 3x3 matrices, how would i get the information of each matrix?

Oh, does it always alternate between a 4x3 and a 3x3 matrix? If so, use this code:

A = dlmread('Nest.txt',' '); %this assumes there are no headers or columns that you don't want
k = 1;
while 1
  n = A(1,1); %this is presumably the number of rows that you want to take after the first line
  X{k,1} = A(2:n+1,:);
  if size(A,1)<n+2
    break
  end
  A = A(n+2:end,:);
  X{k,2} = A(2:n+1,:);
  if size(A,1)<n+2
    break
  end
  A = A(n+2:end,:);
  k = k+1;
  %for each k, we record 2 separate matrices into a cell array
end
Actually, I may still be confused about what you are looking for... In the code that I initially wrote, each station is recorded as a separate matrix in the cell array 'X'. So you will end up with a 1xn cell array.
No, the matrices are as i put them, so it's not necessarily 4x3 than 3x3, it can be any different types of matrices, the thing is the code i have uses the information on these different matrices, so i would need a code to first read the information on the .txt file to then use that information(of each group of matrix) to run through my code one at a time.
Can you provide an example of what the text file looks like? The code I placed as an answer initially should work with the type of data that you posted in your original question.
sure, here it is. It is the same i posted above. Ideally i would place the x,y,z-data in this text file with two or more events and it would run, and better would it be if i didn't have to write these zeros.

If you have data that looks like what you have in your text file, the output from my code is:

X = {[1 1 1; 3 1 1; 2 3 1; 3 2 1], [1 3 1; 2 3 3; 3 1 2]};

I thought this is what you're after...

Oh wow, thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!