xlsread ('not the file name but a string contained in an element of an array that is the file name) - excel

I would like to read an excel file (xlsread) but I don't want to put manually the string every time but instead I want to xlsread the name of the file that is contained in an array.
For example, my array B is:
B =
'john.xlsx'
'mais.xlsx'
'car.xlsx'
Then I would like to read the excel WITH THE NAME that is inside the first element, that means: "john.xlsx"
How can I do this?

data = xlsread(B{1});
Or, if you want to read all of them:
for i=1:length(B)
data(i).nums = xlsread(B{i});
end
Assuming, of course, your B is a cell array. If it's not, it can't exist the way you described it. If all strings have the same length (then it would be possible) or padding with spaces, you can split the char array into a cell array using
B = mat2cell(B,ones(size(B,1),1),size(B,2));

Strings of different lengths would have to be inside a cell array, which you can access elements via the curly brackets {}. So, you can call xlsread on the first element this way:
names{1} = 'john.xlsx';
names{2} = 'mais.xlsx';
names{3} = 'car.xlsx';
num = xlsread(names{1});

Related

MATLAB export data stored in a double array and cell array to a CSV file

I have a MATLAB structure with 19 fields. The main field is a 1 x 108033 double with all values numeric. It looks like this, basically 108033 numbers:
pnum: 5384940 5437561 5570271 5661637 5771155 ...
I have another field called inventors which is a 1 x 108033 cell value. Every cell contains a different number of strings. Columns 1 to 5 for example are
inventors: {2x1 cell} {4x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}
For the first column value, the 2 x 1 cell consists of the following values
5012491-01 and 2035147-03 and so on.
I'd like to jointly export these two to a CSV file. The ideal outcome would repeat the number in pnum so that it establishes a clear link between the pnum and the inventors. Thus, the ideal outcome would look something like this (with the contents of what is in the inventors cell displayed).
pnum inventors
5384940 5012491-01
5384940 2035147-03
5437561 5437561-01
5437561 5437561-02
5437561 5437561-03
5437561 5012491-02
5570271 5437561-03
5661637 1885634-08
5771155 5012491-01
I asked a more complex version of this question before but it was not clear enough what the problem was. Hope it is now.
I'm assuming each cell in inventors is a cell array of strings. It wouldn't make sense for these to be actual floating point or intenger numbers, because the dash would subtract the two numbers separating them together. Now, because you're writing to a CSV file, the easiest thing I can think of is to iterate over each number and cell, then repeat the ID number for as many times as there are elements in a cell. First create the right headers, then write your results. Something like this comes to mind:
f = fopen('data.csv', 'w'); %// Open up data for writing
fprintf(f, 'pnum,inventors\n'); %// Write headers
for ii = 1 : numel(pnum) %// For each unique number
inventor = inventors{ii};
for jj = 1 : numel(inventor) %// For each inventor ID
fprintf(f, '%d,%s\n', pnum(ii), inventor{jj}); %// Write the right combo to file
end
end
fclose(f); %// Close the file
fopen here opens up a file called data.csv so we can write things to it. What is returned is a file pointer called f, which we use to write stuff to this file. After, we write the headers of the file, consisting of pnum and inventors. This is a CSV file so there's a comma separating the two. Now, for each unique number, we then access the right slot in inventors then for each unique inventor, add the same unique ID with the right inventor ID as a line in this file. I use fprintf to write things to file using the associated file pointer established earlier. Once we're done, close the file with fclose.
To double check that this works, I've used the small example you've provided in your post:
pnum = [5384940 5437561 5570271 5661637 5771155];
inventors = {{'5012491-01', '2035147-03'}.', {'5437561-01', '5437561-02', '5437561-03', '5012491-02'}.', {'5437561-03'}, {'1885634-08'}, {'5012491-01'}};
Bear in mind that I don't have access to your struct, so you'll have to access the right fields and assign them to the corresponding variables seen above. So if your struct is called something like data, then you'd do this before you run the above code:
pnum = data.pnum;
inventors = data.inventors;
Running the above code I just wrote and opening up the CSV file (which is called data.csv), I get this:
pnum,inventors
5384940,5012491-01
5384940,2035147-03
5437561,5437561-01
5437561,5437561-02
5437561,5437561-03
5437561,5012491-02
5570271,5437561-03
5661637,1885634-08
5771155,5012491-01

Matlab: sorting strings on size in a struct field

This problem is bugging me and the solution is probably obvious but i cant find it.
I have a bunch of data files which i want to load:
ex_file-1.txt, ex_file-2.txt, ..., ex_file-10.txt
To get their filenames i use:
files = dir('ex_file-*.txt');
This returns a struct with fields name, type, etc. The field name returns:
ex_file-1.txt, ex_file-10.txt, ex_file-2.txt, ..., ex_file-9.txt
I would like to sort this such that ex_file-10.txt is the last file rather than the second.
I have attempted to concatenate, convert to cells and sort but none seem to give what i need. I know that the most obvious solution would be to rename all file names so all strings have the same length but i'd prefer not to do that.
This could be one approach -
%// Input cell array of filenames
names = {'ex_file-1.txt', 'ex_file-10.txt', 'ex_file-2.txt', 'ex_file-3.txt', ...
'ex_file-4.txt', 'ex_file-5.txt'}
%// Reomove the starting common "ex_file" string
stripped_names = strrep(names,'ex_file-','')
%// Remove the ending extension part
stripped_names = strrep(stripped_names,'.txt','')
%// Convert to doubles and then get the sorted indices
[~,idx] = sort(str2double(stripped_names))
%// Use sorted indices to rearrange names array, for the final output
names_out = names(idx)
Code run -
>> names
names =
'ex_file-1.txt' 'ex_file-10.txt' 'ex_file-2.txt' 'ex_file-3.txt' 'ex_file-4.txt' 'ex_file-5.txt'
>> names_out
names_out =
'ex_file-1.txt' 'ex_file-2.txt' 'ex_file-3.txt' 'ex_file-4.txt' 'ex_file-5.txt' 'ex_file-10.txt'
This can be done using regular expressions. The numeric part of file name is detected as a subsequence of numeric characters right before the .txt part.
files = dir('ex_file-*.txt'); %// get file struct array
names = {files.name}; %// get file names. Cell array of strings
numbers = regexp(names, '\d+(?=\.txt)', 'match'); %// strings with numeric part of name
numbers = str2double([numbers{:}]); %// convert from strings to numbers
[~, ind] = sort(numbers); %// sort those numbers
names_sorted = names(ind); %// apply that order to file names
Here is a alternative which does not require any details about the file name. Primary sorting rule shortest first, secondary lexicographic:
%secondary sorting
list=sort(list);
%primary sorting by length
[a,b]=sort(cellfun(#numel,list)):
list=list(b);

MATLAB: Only pick filenames coinciding with some input string

Say I have a directory full of filenames such as:
1242349_blabla.wav
fdp23424_asdf.wav
o2349_0.wav
and I have an input text file listing unique IDs on each newline coinciding with numbers within these filenames (e.g. '23424' for the second filename above).
I'd like to construct a struct of filenames only containing those filenames in that directory that coincide with some ID in the input text file:
fid = fopen('input.txt');
input = textscan(fid, '%s', 'Delimiter', '\n');
filenames = dir(fullfile('/somedir/', '*.wav'));
for i = 1:length(filenames)
for j = 1:length(input)
if (strfind(input{1}(j), filenames(i).name)) ~= [])
% create new struct with chosen filenames
end
end
end
However, I get the error "undefined function 'ne' for input arguments of type 'cell'". I've tried loads of options to no avail. Also, the input evaluates to a 38x1 cell, but which has length 1, so the inner loop will only go once... Any ideas?
Regular expressions are definitely the most flexible and powerful solution. But, if your needs are simpler...you can get away with something simpler, like using wildcards in your dir command. Try something like this:
%get your file IDs from the input file
fid = fopen('input.txt');
input = textscan(fid, '%s', 'Delimiter', '\n');
IDs = input{1};
%loop over each string
myfilenames = {};
for idx = 1:length(IDs)
%get all files build off the given ID
fnames = dir(['somedir/*' IDs{idx} '*.wav']); %wildcards!
%gather the new filenames that match
for Ifname=1:length(fnames)
myfilenames{end+1}=fnames(Ifname).name;
end
end
I would use regular expressions to search for occurrences of the ID in your cell array. Regular expressions are designed to search for patterns in a particular string for you. Because you want to search for specific numbers in a set of strings, I would certainly recommend you use it. Specifically, use the regexp function, and the pattern you want to search for is the ID that you want are searching for.
How regexp works is that you can provide a cell array of strings, and the output will be another cell array where each element is a numeric array that determines the starting index of where the particular pattern you're looking for starts for a particular string in the cell array. Should the array be empty, this means that we didn't find any pattern that matched what you're looking for. If it isn't empty, then it will contain the starting index of where the ID is located in the string. This doesn't really matter - you want to determine whether the ID exists in a particular string, and so checking to see whether each array is empty is what will be useful.
As such, given your filenames that you read through dir, we can create a cell array that stores just the file names themselves, run regexp, then filter out those file names that don't contain the ID you want. Something like this:
f = dir(fullfile('/somedir/', '*.wav'));
filenames = {f.name};
ID = 23424;
check = regexp(filenames, num2str(ID));
filtered_ind = cellfun(#isempty, check);
final_files = f(~filtered_ind);
The first line of code reads the files from your desired directory. The second line of code extracts the names from each name field of the structure as a cell array. The third line is the ID you want to check for. The fourth line does a regexp call on the file names and searches for those file names that contain your desired number. Note that we need to convert the number to a string, as the pattern is expected to be a string. The next line after that finds those filenames that do not have the ID you are looking for, and the last line simply finds those files that do have the ID you're looking for.
You can then go ahead and start your processing. Specifically, you can loop over this cell array and go ahead and create your structures per element in this cell:
for i = 1:length(final_files)
s = final_files(i); %// Get the dir structure for a file that passed the ID check
%// Create your structure now...
%// ...
end
However, you have a series of IDs that you want to check. We can simply take the code above and apply a loop to it. In other words, you'd do something like:
fid = fopen('input.txt');
input = textscan(fid, '%s', 'Delimiter', '\n');
IDs = input{1};
f = dir(fullfile('/somedir/', '*.wav'));
filenames = {f.name};
for idx = 1 : length(IDs)
%// Get an ID
ID = IDs{idx};
%// Do our checking and filter out those files that don't contain our ID
check = regexp(filenames,ID);
filtered_ind = cellfun(#isempty, check);
final_files = f(~filtered_ind);
%// Do your final processing
for i = 1:length(final_files)
s = final_files(i); %// Get the dir structure for a file that passed the ID check
%// Create your structure now...
%// ...
end
end
With the above code, we open the text file, then parse each string that's in the text file and place it into a cell array called IDs. Note here that the IDs are now all strings, so there's no need to do any conversions. After, for each ID we have, we search our filenames to see which files have this ID we're looking for. We filter out those filenames that don't have this ID, then we loop over each one of these files and create our structures. We do this for each ID that we have.
Just to demonstrate that this regexp stuff is working, as a small example, let's use the three filenames you have provided with your post. I've placed these names in a cell array, then I'll run lines 3 to 5 in the code I wrote, then I will filter out those filenames that don't contain the ID we're looking for:
filenames = {'1242349_blabla.wav'; 'fdp23424_asdf.wav'; 'o2349_0.wav'};
ID = 23424;
check = regexp(filenames, num2str(ID));
filtered_ind = cellfun(#isempty, check);
final_filenames = filenames(~filtered_ind);
final_filenames is a cell array our filenames that have our ID. We thus get:
final_filenames =
'fdp23424_asdf.wav'
Good luck!

Storing Matlab data and strings in a tabulated file

I am creating a program which opens an image, and uses the MATLAB ginput command to store x and y coordinates, which are operated on in the loop to fulfill requirements of an if statement and output a number or string corresponding to the region clicked during the ginput session. At the same time, I am using the input command to input a string from the command window relating to these numbers. The ginput session is placed in a while loop so a click in a specific area will end the input session. For each session (while loop), only one or two inputs from the command window are needed. Finally, I am trying to store all the data in a csv or txt file, but I would like it to be tabulated so it is easy to read, i.e. rows and columns with headers. I am including some sample code. My questions are: 1, how can an input of x and y coordinates be translated to a string? It is simple to do this for a number, but I cannot get it to work with a string. 2, any help on printing the strings and number to a tabulated text or cdv file would be appreciated.
Command line input:
prompt='Batter:';
Batter=input(prompt,'s');
While Loop:
count=1;
flag=0;
while(flag==0)
[x,y]= ginput(1);
if (y>539)
flag=1;
end
if x<594 && x>150 && y<539 && y>104
%it's in the square
X=x;
Y=y;
end
if x<524 && x>207 && y<480 && y>163
result='strike'
else
result='ball'
end
[x,y]= ginput(1);
pitch=0;
if x<136 && x>13
%its' pitch column
if y<539
pitch=6;
end
if y<465
pitch=5;
end
if y<390
pitch=4;
end
if y<319
pitch=3;
end
if y<249
pitch=2;
end
if y<175
pitch=1;
end
end
if pitch==0
else
plot(X,Y,'o','MarkerFaceColor',colors(pitch),'MarkerSize',25);
text(X,Y,mat2str(count));
end
count=count+1
M(count,:)=[X,Y,pitch];
end
For the above series of if statements, I would prefer a string output rather than the numbers 1-6 if the condition is satisfied.
The fprintf function is used to print to a file, but I have issues combining the strings and numbers using it:
fileID = fopen('pitches.csv','w');
fid = fopen('gamedata.txt','w');
fmtString = [repmat('%s\t',1,size(Batter,2)-1),'%s\n'];
fprintf(fid,fmtString,Batter,result);
fclose(fid);
for i=1:length(M)
fprintf(fileID,'%6.2f %6.2f %d\n',M(i,1),M(i,2),M(i,3));
end
fclose(fileID);
I have tried adding the string handles to the fprintf command along with the columns of M, but get errors. I either need to store them in an array (How?) and print all the array columns to the file, or use some other method. I also tried a version of the writetable method:
writetable(T,'tabledata2.txt','Delimiter','\t','WriteRowNames',true)
but I can't get everything to work right. Thanks very much for any help.
Let's tackle your questions one at a time:
1, how can an input of x and y coordinates be translated to a string?
You can use the sprintf command in MATLAB. This takes exactly the same syntax as fprintf, but the output of this function will give you a string / character array of whatever you desire.
2, any help on printing the strings and number to a tabulated text or cdv file would be appreciated.
You can still use fprintf but you can specify a matrix as the input. As such, you can do this:
fprintf(fileID,'%6.2f %6.2f %d\n', M.');
This will write the entire matrix to file. However, care must be taken here because MATLAB writes to files in column major format. This means that it will traverse along the rows before going to the next column. If you want to write data row by row, you will need to transpose the matrix first so that when you are traversing down the rows, it will basically do what you want. You will need to keep this in mind before you start trying to write strings to an file. What I would recommend is that you place each string in a cell array, then loop through each element in the cell array and write each string individually line by line.
Hopefully this helps push you in the right direction. Reply back to me in a comment and we can keep talking if you need more help.

How to store string matrix and write to a file?

I don't know if Matlab can do this, but I want to store some strings in a 4×3 matrix, each element in the matrix is a string.
test_string_01 test_string_02 test_string_03
test_string_04 test_string_05 test_string_06
test_string_07 test_string_08 test_string_09
test_string_10 test_string_11 test_string_12
Then, I want to write this matrix into a plain text file, either comma or space delimited.
test_string_01,test_string_02,test_string_03
test_string_04,test_string_05,test_string_06
test_string_07,test_string_08,test_string_09
test_string_10,test_string_11,test_string_12
Seems like matrix data type is not capable of storing strings. I looked at cell. I tried to use dlmwrite() or csvwrite(), but both of them only accept matrices. I also tried cell2mat() first, but in that way all letters in the strings are comma seperated, like
t,e,s,t,_,s,t,r,i,n,g,_,0,1,t,e,s,t,_,s,t,r,i,n,g,_,0,2,t,e,s,t,_,s,t,r,i,n,g,_,0,3
So is there any way to achieve this?
It is possible to shorten yuk's solution a bit.
strings = {
'test_string_01','test_string_02','test_string_03'
'test_string_04','test_string_05','test_string_06'
'test_string_07','test_string_08','test_string_09'
'test_string_10','test_string_11','test_string_12'};
fid = fopen('output.txt','w');
fmtString = [repmat('%s\t',1,size(strings,2)-1),'%s\n'];
fprintf(fid,fmtString,strings{:});
fclose(fid);
Cell array is the way to store strings.
I agree it's a pain to save strings into a text file, but you can do it with this code:
strings = {
'test_string_01','test_string_02','test_string_03'
'test_string_04','test_string_05','test_string_06'
'test_string_07','test_string_08','test_string_09'
'test_string_10','test_string_11','test_string_12'};
fid = fopen('output.txt','w');
for row = 1:size(strings,1)
fprintf(fid, repmat('%s\t',1,size(strings,2)-1), strings{row,1:end-1});
fprintf(fid, '%s\n', strings{row,end});
end
fclose(fid);
Substitute \t with , to get csv file.
You can also store cell array of strings into Excel file with XLSWRITE (requires COM interface, so it's on Windows only):
xlswrite('output.xls',strings)
In most cases you can use the delimiter ' ' and get Matlab to save a string into file with dlmwrite.
For example,
output=('my_first_String');
dlmwrite('myfile.txt',output,'delimiter','')
will save a file named myfile.txt containing my_first_String.

Resources