How can I select a value from a filename and write a different value to a structure?

1 view (last 30 days)
Hello All,
Firstly, my apologies for not being able to give you some example code here - a large part of the problem I am having is that I am a total novice with Matlab/coding, and I'm not sure even of the correct commands that will help me resolve this, so despite reading many forum entries and documentation - I just cant get a handle on what I need to write to get this to work!
With a lot of help from a colleague (who is no longer available to offer guidance unfortunately), I have created a way to enter several data files (.txt) at a time, and for different values to be drawn down from the raw data and to be stored in a structure for later use.
Now, roughly speaking what I want to happen is that if the filename.txt has a particular value at a particular point in the filename, then a different value is assigned to the structure. This is because different values in the filename means that the raw data has a different classification - so in effect the filename is coded.
For example, 1_filename.txt would mean the data is "green" data, 2_filename.txt would mean the data is "amber" data and so on. Therefore, if a file is imported in that is prefixed with a "1", then a field in the structure (which is called "filestore") would indicate "green" under a column header that is called "dataclass". This would need to be repeated for all raw data entries relative to how many were imported (so, I think 'length' needs to feature here?) and and their respective prefix would then generate a different entry for each row in the "filestore" structure.
I really hope that makes sense and that some of you will be kind enough to help me with this. I am more then happy to try as many iterations as you advise until this works!
Kind regards,
10B.

Accepted Answer

dpb
dpb on 4 Dec 2014
This is simple enough to parse the filename; just need to know where it is and how it's stored. I tend to process sequences of files using dir to return the structure with the desired names by using appropriate wildcard in the call so I'll demonstrate that...
clrs={'green';'amber'; ...}; % the table in sequence of the coding
d=dir('*filename.txt'): % get the files that match
for i=1:length(d)
clr=clrs(sscanf(d(i).name,'%d')); % lookup the color in the table based on position
filestore(i).dataclass=clr;
...whatever you do with the data in the file here...
end
  2 Comments
dpb
dpb on 9 Dec 2014
And, of course, you can get rid of the intermediary...
for i=1:length(d)
filestore(i).dataclass=clrs(sscanf(d(i).name,'%d'));
...

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!