Could someone please help me, I have 40 txt files which I want to read into matlab so that each one will be displayed as a matrix (each file has 5 columns and 2025 rows). If I run this code, then txtfiles and j is displayed correctly, but T shows me just one matrix.... Thanks in advance!
txtfiles=dir('*.txt');
for j=1:length(txtfiles)
T=readmatrix(txtfiles(j).name)
end
you can instead write in the loop T as a cell or as an array, for example:
...
T{j}=...
...
or
T(:,:,j)=...
Related
I try to process data from an excel file from several sheets (~200). Luckily the data is always at the same position in each sheet. I wrote the following code for this purpose which unfortunately does not work since I have problems with converting the cell entries to a matrix. Any idea how to solve that? Thanks!
[~, sheet_name] = xlsfinfo('mydata.xlsx');
for k=1:numel(sheet_name)
data{k}=xlsread('mydata.xlsx',sheet_name{k},'A7:A14');
b{k}=cell2mat(data{k}) %this line does not work...
end```
You are not using the cell2mat command on the whole cell.
data{k}
will already give you a matrix that contains the 7 entries of the sheet k.
You either need to use cell2mat on the whole cell after the loop:
b=cell2mat(data)
or
b(k)=data{k}
within the loop.
I have made a script which contains a for loop selecting columns from 533 different excel files and places them into matrices so that they can be compared, however the process is taking too long (it ran for 3 hours yesterday and wasn't even halfway through!!).
I know xlsread is naturally slow, but does anyone know how I can make my script run faster? The script is below, thanks!!
%Split the data into g's and h's
CRNum = 533; %Number of Carrington Rotation files
A(:,1) = xlsread('CR1643.xlsx','A:A'); % Set harmonic coefficient columns
A(:,2) = xlsread('CR1643.xlsx','B:B');
B(:,1) = xlsread('CR1643.xlsx','A:A');
B(:,2) = xlsread('CR1643.xlsx','B:B');
for k = 1:CRNum
textFileName = ['CR' num2str(k+1642) '.xlsx'];
A(:,k+2) = xlsread(textFileName,'C:C'); %for g
B(:,k+2) = xlsread(textFileName,'D:D'); %for h
end
Don't use xlsread if you want to go through a loop. because it opens excel and then closes excel server each time you call it, which is time consuming. instead before the loop use actxserver to open excel, do what you want and finally close actxserver after your loop. For a good example of using actxserver, search for "Read Spreadsheet Data Using Excel as Automation Server" in MATLAB help.
And also take a look at readtable which works faster than xlsread, but generates a table instead.
The most obvious improvement seems to be to load the files only partially if possible. However, if that is not an option, try whether it helps to only open each file once (read everything you need, and then assign it).
M(:,k+2) = xlsread(textFileName,'C:D');
Also check how much you are reading in each time, if you read in many rows in the first file, you may make the first dimension of A big, and then you will fill it each time you read a file?
As an extra: a small but simple improvment can be found at the start. Don't use 4 load statements, but use 1 and then assign variables based on the result.
As mentioned in this post, the easiest thing to change would be to set 'Basic' to true. This disables things like formulas and macros in Excel and allows you to read a simple table more quickly. For example, you can use:
xlsread('CR1643.xlsx','A:A', 'Basic', true)
This resulted in a decrease in load time from about 22 seconds to about 1 second for me when I tested it on a 11,000 by 7 Excel sheet.
I want to save data in a results.xls file which I want to set its first row "header" with a specific names lets say a,b,c,d,e. So, basically I have matlab function func1 which loops n times. In this loop, I call another function func2, where I do some processes and save save variables lets say a_res,b_res, c_res,d_res,e_res, I want to save those variables in each iteration in the results.xls file, where if the loop has 10 iterations then this means the results file will have 10 rows and 5 columns + the header row, so 11 in total. Could anyone please advise how this can be done in Matlab?
the below image shows the desired output where the first row is headings, then each row after that will be filled with variables calculated in each iteration of the loop.
EDIT:
Following the proposed solution, I used this:
save('results.xls', 'name','number_of_points','blood_level','width','sugval', '-ASCII');
where 'name','number_of_points','blood_level','width','sugval', are variables holding strings. But when I open the excel file this is what I get results.xls if anyone could please advise.
p = rand(1, 10);
q = ones(10);
save('test.xls', 'p', 'q', '-ASCII')
This works for me. It saves me on the first row the random values of "p" and beneath I have the values for "q". You might check it out. Hope it helps
I have a *.mat file with some tables included.I want to load each table one by one and then plot it so that the figures of each table update on one single figure .Please tell me if i am not clear ...I though this code must have been enough but it is not working when i asked it to load one table ... please guide me why and how i should improve the code ?
Thank you
filename = uigetfile
load(filename)
c=who('Hs*' ) % all the tables which starts with Hs
cc=numel(c) % count the number of desired tables in MAT file which we loaded before
for i=1: cc
b=load(filename,c(i));
contour(b,60)
end
I can see at least three errors in your code.
After the first load you don't clear the mat file, and hence all tables are still in the memory.
c is a cell array, and you shoud index it by using brackets (i.e. c{i}).
By default each plot in a figure deletes the previous one. Writing figure(); hold on; before the for loop shoud fix your problem.
I have a large text file with 2 columns where the values are separated by commas. I am trying to create a simple program which allows to plot a graph with the data extracted for every 3 rows consecutively until reaching the end of the file.
The first 9 rows of my file can be seen below:
115,1.2
324,3.4
987,1.2
435,-2.3
234,1.4
278,1.3
768,3.4
345,-1.3
126,3.6
I have been reading that with 'Textread' I can write my data into multiple outputs and then I can use 'plot', to plot the previous generated outputs on a graph. I know that I will need some loop whiles to repeat the process and indicate the end of the file, etc. But I am struggling to find the way to do this:-(.
I have only managed plotting a graph for the first 3 rows of my file (see code below), but I need this process to be repeated until the end of the file.
[Codes,Values]=textread('MyData.txt','%3u %f',3,'delimiter',',','emptyvalue',NAN); %//Reads the first three rows of my file and stores the values in 2 variables
figure
plot(Codes,Values) %//plots a graph with the variables obtained before
saveas(gcf,'Graph.pdf') %//Saves the created graph in a pdf format file.
I would be very grateful if somebody could help me.
I have finally found out a way. I paste here the code that allows me plotting a graph every three lines from my text file.
[Codes,Values]=textread('MyData.txt','%3u %f','delimiter',',','emptyvalue',NAN); %//Reads my text file and stores the values in 2 variables
nrows=1;
conta=1;
contb=3;
contc=1;
contd=3;
nfig=1;
while nrows<=size(Codes,1)
if contb<=size(Codes,1)
Codes1=Codes(conta:contb,1);
Values1=Values(contc:contd,1);
figure
plot(Codes1,Values1) %//plots a graph with the selected rows.
saveas(gcf,strcat('Graph', num2str(nfig),'.pdf')) %//Saves the created graph in a pdf format file.
else
Codes1=Codes(conta:contb,1);
Values1=Values(contc:contd,1);
figure
plot(Codes1,Values1) %//plots a graph with the selected rows.
saveas(gcf,strcat('Graph', num2str(nfig),'.pdf'))
end
nrows=nrows+3;
conta=conta+3;
contb=contb+3;
contc=contc+3;
contd=contd+3;
nfig=nfig+1;
end
You mentioned that it is a large text file, but will it an issue to load the whole text file and then simply sample every third row, for example, as follows:
[Codes, Values] = textread('MyData.txt','%3u %f','delimiter',',','emptyvalue',NAN);
rowstokeep = 1:3:length(Values) % every third line
plot(Codes{rowstokeep}, Values{rowstokeep}); % plot just the rows you wanted