how to change variable name in loop

2 Ansichten (letzte 30 Tage)
Ziga Debevec
Ziga Debevec am 23 Jun. 2016
Bearbeitet: Stephen23 am 19 Jun. 2019
Hi, so far I have created a code that reads several .txt files and put the into cell array, see the code below.
clc
clear all
close all
x='Vnesi stevilo .txt datotek za analizo ';
NumFiles=input(x)
for m=1:NumFiles
filename=sprintf('meritev%d.txt',m);
fileID=fopen(filename, 'r');
pc{m}=textscan(fileID,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','Delimiter','\t');
fclose(fileID);
end
p1=cell2mat(pc{1});
Now I am trying to specify name for each column for example, first column of the p1 would be T_1_1. I would like to create the loop that would input names so that the first coulmn of the p2 would be T_1_2 and so on. All the text files are made of the same numbers of columns and rows. Is possible to do something like that :D?
Many thanks,
Ziga

Akzeptierte Antwort

Stephen23
Stephen23 am 23 Jun. 2016
Bearbeitet: Stephen23 am 19 Jun. 2019
  2 Kommentare
Ziga Debevec
Ziga Debevec am 23 Jun. 2016
wow, many thanks for your answer! the fact is, that I need to do the same mathamatical operations on each text file I would like to import in Matlab but the calculation steps are the same for all text files (which are also the same, except the numbers in columns are different). Then, I would like to copare calculations on graph, that is why I thought it would be the best to create a loop that would name the variables from column for me. As I read your post, this is not the best idea. Any suggestions what would be most time efficient and simple :D?
Stephen23
Stephen23 am 23 Jun. 2016
Bearbeitet: Stephen23 am 19 Jun. 2019
Use indexing.
In a nutshell: preallocate one array, and then use indexing in the loop to allocate the data from each file to a different position in that variable.
Something like this (pseudocode):
C = cell(..);
for k = 1:N
C{k} = get kth file
end
and bingo, all of your data is in C, and can be accessed using indexing (so you can loop over it and do all sorts of other magic).
This is an extremely common topic, so why not take the opportunity to do some reading and learning:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ziga Debevec
Ziga Debevec am 23 Jun. 2016
Hi, thanks for your answer, but still don't know how to first make that the first column of p1 would be T_1_1 and then the first column of p2 (which is acctualy pc{2}) would be T_2_2. The loop you posted only creatin variables, but not adjusting them to columns?
Tnx, Ziga

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by