1

I basically have a large data set file and I want to write a MATLAB script that creates a data structure for it. I have tried to read about using structured arrays in MATLAB, but I haven't found a solution of how to do this. I don't really have a lot of experience in writing scripts on MATLAB.

Edited: My data set is a large list of items with, say, 10 different characteristics of each item written down. So for example, say 100,000 listings of houses and characteristics given could be price, county, state, date when sold, etc. This file is in a txt., xls., or any format you like to play with.

I would like to write a MATLAB script that creates a data structure of it say in the format:

house(i).price    
house(i).county
house(i).state
house(i).date

etc

Any suggestions to the right direction or examples of teaching how to do this would be greatly appreciated.

6
  • 1
    I would appreciate if the person who down voted could explain why he did so, and possibly give a suggestion of how to improve my question. Thanks. Commented Nov 18, 2014 at 19:18
  • 1
    I didn't downvote but your question is very broad. Providing specifics and examples of what type(s) of data you have, how it is stored, and how you want to store it would be very helpful. Otherwise we can't say much beyond "here's a structure, you should use it," which you've already figured out. Commented Nov 18, 2014 at 19:26
  • @excaza. Thanks. Maybe the edit that I wrote would give more context? Commented Nov 18, 2014 at 19:35
  • @ThomasE. it seems like you already understand the basic syntax for structures, so what is your question? Is it loading in your data? As before, there are many different methods and it's going to be heavily based on how your data is stored and how much automation you want. Do you want to define your own field names? Pull the field names from your data headers? Commented Nov 18, 2014 at 19:50
  • @excaza. Right now my data is just a list of rows and each characteristic is separated by a comma. So for example, first row could be the list of all the characteristics: price, county, state, date. And the second row would be: 200, Kings, NY, 01012000. Third row would be another listed item, etc. I would like to create a data structure as written above but I don't know how to really do it in practise and what the script should look like. Also, I have no idea how to load my data to it. We can assume that my data is in txt. or csv. format. Commented Nov 18, 2014 at 20:18

1 Answer 1

1

This seems like a very reasonable question, and one that can be easily addressed.

The format of the file, really makes this problem easy or hard. I really don't like .xls files for this kind of work myself, but I realize, you get what you get. Let's assume it's in a tab delimited text file like:

Price   County  State   Date
100000  Sherlock    London  2001-10-01
134000  Holmes  Dartmoor    2011-12-30
123456  Watson  Boston  2003-04-15

IfI would just read the whole thing into an parse the field name row and use dynamic structure naming to make the array of structures.

fid = fopen('data.txt','r');

tline = fgetl(fid);
flds = regexp(tline,'\s*','split');
% initialize the first prototype struct
data = struct();
for ii=1:length(flds)
  data.(flds{ii}) = [];
end
ii = 1;
% get the first line of data
tline = fgetl(fid);
while ischar(tline)
  % parse the data
  rowData = regexp(tline,'\s*','split');
  % we're assuming no missing data, etc
  % populate the structure
  for jj=1:length(flds)
    data(ii).(flds{jj}) = rowData{jj};
  end
  % since we don't know how many lines we have
  % we could figure that out, but we won't now
  % we'll just use the size extending feature of
  % matlab arrays, even though it's slow, just
  % to show how we would do it
  tline = fgetl(fid);
  ii = ii + 1;
end
fclose(fid)

Hope this gets you started!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Your answer has been helpful and I'm going through it to understand it. Thanks a lot.
when I want to add the variables Price, county, state, date, where exactly in the code should I insert those?
After the fclose, but not sure I follow your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.