Reading the sheet name of a .xls file with Matlab - matlab

I have got 30 files named Data1.xls to Data30.xls. In each file, there are two sheets I'm interested in. The first is called 'Ergebnisse' where I get the name of the second sheet, which is important to me. This sheet changes its name. My problem here is that I don't know how to tell Matlab to use the changing sheet name.
What i got so far:
liste = dir('*.xls'); % how many files in the folder
liste=struct2cell(liste);
liste=liste(1,:)';
for i=1:length(liste) % i=number of files
filename=['Data' num2str(i) '.xls'];
[num,txt,raw]=xlsread(filename,'Ergebnisse');
sheet=txt(3,1);
[num,txt,raw]=xlsread(filename,sheet);
end
The answer for sheet is 'T4_quer_3' which I would normally write into the next xlsread but it doesn't work.
Thanks for your help

you dont need the cell txt(3,1), but its content. so either go for
sheet=txt{3,1};%notice the other brackets
or you go for
[num,txt,raw]=xlsread(filename,sheet{:}); %{:}content of a cell

Related

Xlsread sheet in for loop

I have a number of excel sheets I would like to cycle through enclosed in a for loop, with sheet name a through A to X. Is this possible?
I tried this:
for letter='A':'X'
[num,txt,raw] = xlsread('Grouting_sum_final.xlsx','%s',letter);
% Lots of code below here (not relevant for the problem)
end
Yes, it is, but you do not need the '%s' part of your line.
If you go to the documentation website, you will find that you have to pass as first argument the excel file name and as second the sheet name.
So your code should read something like:
for letter='A':'X'
[num,txt,raw] = xlsread('Grouting_sum_final.xlsx',letter);
% Lots of code below here (not relevant for the problem)
end
Also, I am assuming you are aware that you keep on overwriting your data retrieved from the Excel sheet.

How to use 'fprintf' to show the output in a txt file and save it instead of command window in Matlab?

I've been trying to make the output shown in the text file instead of the command window. Im blur right now as i already look at a lot of example but it always show the error on fprintf. I try to edit the code of fprintf(fid,'%s\n',word);%Write 'word' in text file (upper) in one of the Matlab example which is Automatically Detect and Recognize Text in Natural Images.
This is the link of the code.
https://www.mathworks.com/help/vision/examples/automatically-detect-and-recognize-text-in-natural-images.html?s_tid=srchtitle
Basically the above link display the output on the command window. But, i need it to be on the txt file.
Im really new to this, i want to know what code do i need to put, how and where should i put the fprintf to make the output shown on the text file and not on the command window.
Also, can i save the text file after that? do i to put any additional code?
I really need your help. Thank u in advance!
It seems you're looking for the fopen() method. It takes two parameters, the first being the name of the file you'd like to write to, and the second being the mode. If the file specified does not exist in the root directory, it will be created on execution.
fileID = fopen('exp.txt','w');
fprintf(fileID, fid,'%s\n', word);
fclose(fileID); % Make sure to always close the stream after finishing
More on fopen() here

How to save a mat file in another directory in matlab

I want to save a matrix (e.g. "PTX_Data_Raw.mat") in another folder (e.g. Temp folder). I have written below code:
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp');
save(filename,'PTX_Data_Raw.mat');
but it didn't work. Does anybody can help me for solving this problem?
THX
Going with your comments, you are using save wrong. The first parameter is the filename you want to call the MAT file and second parameter and onwards are the variables you want to save.
Therefore, you need to make sure filename contains the entire filename, including the path followed by the actual name of the MAT file you want. After, the second parameter is PTX_Data - the name of the matrix you want to save.
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
%// Change
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp\PTX_Data_Raw.mat');
save(filename,'PTX_Data'); %// Change

ReadTable in Matlab

When I use the readtable function I get the following error:
IVcellData = readtable('RiskModelData','Sheet',2,'Range','A1:A49')
Error using readtable (line 129) Invalid parameter name: Sheet.
Would appreciate if anyone could help me.
Have you renamed Sheet 2 to something else, e.g. Datafile? If so, you need to use this name (inside single quotes) not the sheet number instead of 2 in that call.
Also, you need to make a call to
opts = detectImportOptions(yourfilename)
before the call to readtable. I suspect this one is this cause as it is not recognising Sheet as a variable.
Took me a while to discover that lot, mostly empirical as the documentation is not clear on that point.
Keith
Looks like you need to define extension:
T = readtable(filename) creates a table by reading column oriented data from a file.
readtable determines the file format from the file name's extension:
.txt, .dat, or .csv for delimited text files
.xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, or .ods for spreadsheet files
try ReadModelData.xls or .xlsx

In Matlab, how do I create a CSV file from a subset of the lines in a text file?

I need to open a text file and convert it into a CSV file in Matlab. The first 3 lines of the text file are sentences that need to be omitted. The next 28 lines are numbers that need to make up the first column of the CSV, and then the next 28 lines need to make up the second column.
The text file is called datanal.txt and the output file can be named anything. Any help would be appreciated.
Don't have Matlab now to test, but try this. Your input file should be in Matlab's current directory, or put the full path to the file name.
A = csvread('datanal.txt',3,0);
A = reshape(A,28,2);
csvwrite('output.csv',A)
well you can add #'s in front of the first 3 lines then use load and a reshape. Did you need a fully automated script or is there only one file? If you're familiar with matlab at all there are a bunch of ways to turn that large column vector into a matrix.