Readtable then save in workspace with a new name based on a variable
9 views (last 30 days)
Show older comments
Rose Montgomery
on 10 Jan 2018
Commented: Rose Montgomery
on 10 Jan 2018
I have a large number of excel spreadsheets and want to extract data for comparisons and graphs. I don't want to generate more saved files, but I do want to load the information from each file into the workspace for manipulation. I'm getting each set of data using a loop. Once I have it, I need to rename the table so that when I loop around for the next set of data it isn't overwritten. I'd like to use a name from a list of identifiers (RodList) which I've moved to a string as "rodname". I'm sure there is a simple way to do this...I'm a newbie...any help would be much appreciated!
for next = 0:0
nextfile = RodList(next + 1); % index to next rod in the list
sourcedir = 'C:\Users\rnm\Scans\';
sourceext = '.xlsx';
rodname = strcat(nextfile)
Filetoprocess = strcat(sourcedir,nextfile,sourceext);
FileData0deg = readtable(Filetoprocess{:},'Sheet','Sheet1','Range','D1:E5000');
FileData0deg = rmmissing(FileData0deg);
_ _I need to keep FileData0deg in the workspace as rodname_FileData0deg__
end
3 Comments
Stephen23
on 10 Jan 2018
Edited: Stephen23
on 10 Jan 2018
"Unfortunately, you didn't provide any links for how to do that in my particular application..."
Errrrr... all three links show or refer to pages with a cell array, a loop, and indexing. That is really all you need.
Try the first link that I gave you. It clearly shows how to use a cell array and indexing. No dynamic variable names are required. See Sean de Wolski's answer for another example of this (you will see that it is basically the same as the example in the first link).
"...without creating a dynamic variable. Still in the dark here..."
The whole point is to avoid magically creating or accessing variable names.
" I have been successful in using sprintf, I don't want to save a bunch of new files"
Totally irrelevant. What files data is stored in and how that data exists in MATLAB memory are two totally different things. Nothing in using a cell array (or dynamic variable names for that matter) has any effect on what files you have.
I could have one matrix A and save each of its rows in a separate file: the files and its existence in MATLAB memory are quite independent to each other. There is nothing in how you manipulate that data inside MATLAB that means you would have to create new files that "would clutter my drive and would be slow".
Accepted Answer
Sean de Wolski
on 10 Jan 2018
Edited: Sean de Wolski
on 10 Jan 2018
Use a cell array that is the size of the number of files and then read into it.
files = dir(*.xlsx');
filedata = cell(numel(files), 1)
for ii = 1:numel(files)
filedata{ii} = readtable(files(ii).name, ETC.)
end
Something along these lines. Now the fifth element in the cell is the content of the fifth Excel sheet.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!