Can MATLAB read a file inside a file?

Hi all. I have MATLAB reading in a text file successfully into a structure. But I would like for my text file to point to csv file to read in.
So I read in the text file data and it is a list of parameters. but one of the parameters listed is another read-in file.
*Edit*
I am using a file that reads in and parses the data based on type. This is what it looks like:
function result = load_data( r )
%LOAD_DATA recursively convert java objects
if isa(r, 'char')
result = char(r);
elseif isa(r, 'double')
result = double(r);
elseif isa(r, 'logical')
result = logical(r);
Is there a way I can say something like "elseif isa(r, 'csv, text, or yaml file, etc') ?
Thanks in advance!

 Accepted Answer

Yes, it is possible, but there is no function that will do this on its own. You will need to write your own code to parse the first file and then perform the desired operations with the data it has loaded. This could be a second read file operation using a file name obtained from the first file.

6 Comments

I am using a code that parses, and this part seems important because it loads data based on type:
function result = load_data( r )
%LOAD_DATA recursively convert java objects
if isa(r, 'char')
result = char(r);
elseif isa(r, 'double')
result = double(r);
elseif isa(r, 'logical')
result = logical(r);
I was wondering if there is a way I can edit this to say something like " elseif isa(r, 'csv file') or text file, or yaml file etc.
I will edit my questions based on these new findings.
What do you think?
You don't need to edit the question. Just add clarifying comments to the original question.
I do not believe isa is able to check anything other than MATLAB data types, and a csv file is not.
How is the second file stored in the first file? I was assuming it was a string. It mgiht be helpful to actually see an example of what the data looks like in the first file.
I will generalize it a lot, but the first file looks like:
Name: name
Type: type
Size: 10
Table: (this is the part I'm a bit confused on. if I should put in a filename or a pathing)
(etc.)
And so far, MATLAB is able to read in the char and doubles types from file #1 as they are meant to be into a struct. And I wanted to read in the data from file #2 into a struct, cell, or matrix. that part i will figure out when the time comes. As long as I can read in the contents of file #2.
I hope this makes sense
Generalizing is not helpful, as it doesn't help me know what you would have to do in MATLAB to get it to work. Please share specifics.
Sorry, you asked how the data was stored in my first file. When I said I am generalizing, I meant that I was changing the word usage but this is the exact format I'm given. The function I am using knows to just take what is to the right and read it in as such. So Name will be 'char' and size will be 'double'. Right now when I try to run it like this (below), it reads in 'file.csv' as a string.
I was thinking about trying an elseif statement to recognize when the str includes .csv in the string, and then read in that string as a file. I don't know how to go about that but I am trying
Name: name
Type: type
Size: 10
Table: 'file.csv'
You doen't read that string in as a file. You read it in as a string. Then you have some follow up code that then reads in the csv file using that name captured in the string.
Consider using fileparts.
[~,f,x] = fileparts('file.csv')
f = 'file'
x = '.csv'

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!