How to save a structure array to a text file - arrays

In MATLAB how do I save a structure array to a text file so that it displays everything the structure array shows in the command window?

I know this thread is old but I hope it's still going to help someone:
I think this is an shorter solution (with the constraint that each struct field can contain scalar,arrays or strings):
%assume that your struct array is named data
temp_table = struct2table(data);
writetable(temp_table,'data.csv')
Now your struct array is stored in the data.csv file. The column names are the field names of a struct and the rows are the different single-structs of your struct-array

You have to define a format for your file first.
Saving to a MATLAB workspace file (.MAT)
If you don't care about the format, and simply want to be able to load the data at a later time, use save, for example:
save 'myfile.mat' structarr
That stores struct array structarr in a binary MAT file named "file.mat". To read it back into your workspace you can use load:
load 'myfile.mat'
Saving as comma-separated values (.CSV)
If you want to save your struct array in a text file as comma-separated value pairs, where each pair contains the field name and its value, you can something along these lines:
%// Extract field data
fields = repmat(fieldnames(structarr), numel(structarr), 1);
values = struct2cell(structarr);
%// Convert all numerical values to strings
idx = cellfun(#isnumeric, values);
values(idx) = cellfun(#num2str, values(idx), 'UniformOutput', 0);
%// Combine field names and values in the same array
C = {fields{:}; values{:}};
%// Write fields to CSV file
fid = fopen('myfile.csv', 'wt');
fmt_str = repmat('%s,', 1, size(C, 2));
fprintf(fid, [fmt_str(1:end - 1), '\n'], C{:});
fclose(fid);
This solution assumes that each field contains a scalar value or a string, but you can extend it as you see fit, of course.

To convert any data type to a character vector as displayed in the MATLAB command window, use the function
str = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(yourArray);
You can then write the contents to a file
fid = fopen('myFile.txt', 'w');
fwrite(fid, str, '*char');
fclose(fid);

Related

Extract Data From NetCDF4 File Using List

I am using a list of integers corresponding to an x,y index of a gridded NetCDF array to extract specific values, the initial code was derived from here. My NetCDF file has a single dimension at a single timestep, which is named TMAX2M. My code written to execute this is as follows (please note that I have not shown the call of netCDF4 at the top of the script):
# grid point lists
lat = [914]
lon = [2141]
# Open netCDF File
fh = Dataset('/pathtofile/temperaturedataset.nc', mode='r')
# Variable Extraction
point_list = zip(lat,lon)
dataset_list = []
for i, j in point_list:
dataset_list.append(fh.variables['TMAX2M'][i,j])
print(dataset_list)
The code executes, and the result is as follows:
masked_array(data=73,mask=False,fill_value=999999,dtype=int16]
The data value here is correct, however I would like the output to only contain the integer contained in "data". The goal is to pass a number of x,y points as seen in the example linked above and join them into a single list.
Any suggestions on what to add to the code to make this achievable would be great.
The solution to calling the particular value from the x,y list on single step within the dataset can be done as follows:
dataset_list = []
for i, j in point_list:
dataset_list.append(fh.variables['TMAX2M'][:][i,j])
The previous linked example contained [0,16] for the indexed variables, [:] can be used in this case.
I suggest converting to NumPy array like this:
for i, j in point_list:
dataset_list.append(np.array(fh.variables['TMAX2M'][i,j]))

How to Separate Commas and save the values in a Matrix Array.

I have the following dataset and i want to separate the txt values and commas and save to a 10x18 matrix.
I tried with str2mat(char(txt(...)) but it didn't help. It separates the value and make individual character. How can I save the data in matrix.
'1,4,5,12,13,17,22,24,25,21,20,17,12,12,10,9,8,4'
'2,4,6,7,9,10,13,15,17,17,14,13,12,11,9,7,5,2'
'3,7,8,10,11,13,14,15,18,18,16,15,14,11,10,5,4,2'
'5,5,8,9,15,17,18,20,21,22,21,17,15,14,12,11,6,2'
'5,6,8,10,14,15,19,21,24,19,17,16,14,13,10,5,3,2'
'4,6,7,9,10,13,17,19,20,20,19,17,16,12,11,9,7,1'
'6,7,12,16,19,20,21,22,24,24,22,17,9,6,4,3,2,1'
'4,8,10,11,14,16,18,22,24,22,21,19,18,13,8,6,5,4'
'1,3,11,12,14,15,17,18,22,21,20,16,15,14,11,7,6,2'
'2,5,11,15,18,19,22,26,27,25,22,17,9,8,7,4,2,1'
As simple as:
text = '1,2,3':
nums = strsplit(text,',');
vals = str2double(nums);

How to extract struct element name given part of the name?

I am currently post-processing a lot of .mat files (300+) created with a long simulation (3days+).
Each of these mat files contain several variables, each named after their position on a grid (yes, they've been created with eval).
I thus created a script that opens each of the files sequentially
s = what(pwd);
s = s.mat;
for i=1:length(s)
data = open(sprintf('%s',s{i,:}));
% here goes what I would like to do
end
What I am trying to do now is to extract the current name of the data component that fits a certain pattern.
Specifically, I know that in data there is a vector named coef_%i_%i and I would like to isolate it and assign it to a multi-dimensional array.
The second part I know how to do it, I can scan the variable name for the _ characters, isolate the integers and assign the vector to its appropriate location in the array.
My question is:
Is there in matlab a way to do something along the lines of vectorname = extractname('data.coef*');?
Assuming you have something like:
clear data
% create a dummy field
name = sprintf ( 'coef_%i_%i', randi(100,[2 1]) );
% a data struct with a field which starts "coef*"
data.(name) = rand;
% and your data field may contain some other fields?
data.other = [];
You can then extract out the fields which contain the coef string
function output = extractname ( data )
%extract the fieldnames from data:
fn = fieldnames(data);
% find all that have coef in them
index = cellfun(#isempty, strfind(fn,'coef'));
% remove any that dont have coef in them
fn(index) = [];
%You are then left with one(or more) that contain coef in the name:
output = fn;
end
If your data struct contains fields which may have "coef" elsewhere in the name you would need to go through them and check if coef is at the start. You should also check that at the end of your function that one and only one field has been found.

Best way to compare data from file to data in array in Matlab

I am having a bit of trouble with a specific file i/o in matlab, I am fairly new to it still so some things are still a bit of a mystery to me. The input file is structured as so:
File Name: Processed_kplr003942670-2010174085026_llc.fits.txt
File contents- 6 Header Lines then:
1, 2, 3
1, 2, 3
basically a matrix of about [1443,3] with varying values
now here is the matrix that I'm comparing it to:
[(0123456, 1, 2, 3), (0123456, 2, 3, 4), (etc..)]
Now here is my problem, first I need to know how to properly do the file input in a way which can let me compare the ID number (0123456) that is in the filename with the ID value that is in the matrix, so that I can compare the other columns of both. I do not know how to achieve this in matlab. Furthermore, I need to be able to loop over every point in the the matrix that matches up to the specific file, for example:
If I have 15 files ranging from 'Processed_0123456_1' to 'Processed_0123456_15' then I want to be able to read in the values contained in 'Processed_0123456_1'and compare them to ANY row in the matrix that corresponds to that ID (0123456). I don't know if maybe accumaray can be used for this, but as I said I'm not sure.
So the code must:
-Read in file
-Compare file to any point in the matrix with corresponding ID
-Do operations
-Loop over until full list of files in the directory are read in and processed, and output a matrix with the results.
Thanks for any help.
EDIT: Exact File Sample--
Kepler I.D.-----Channel
[1161345]--------[84]
-TTYPE1--------TTYPE8------------TTYPE4
['TIME']---['PDCSAP_FLUX']---['SAP_FLUX']
['BJD - 2454833']--['e-/s']--------['e-/s']
CROWDSAP --- 0.9791
630.195880143,277165.0,268233.0
630.216312946,277214.0,268270.0
630.23674585,277239.0,268293.0
630.257178554,277296.0,268355.0
630.277611357,277294.0,268364.0
630.29804426,277365.0,268441.0
630.318476962,277337.0,268419.0
630.338909764,277403.0,268481.0
630.359342667,277389.0,268463.0
630.379775369,277441.0,268508.0
630.40020817,277545.0,268604.0
There are more entries than what was just posted but they go for about 1000 lines so it is impractical to post that all here.
To get the file ID, use regular expressions, e.g.:
filename = 'Processed_0123456_1';
file_id_str = regexprep(filename, 'Processed_(\d+)_\d+', '$1');
file_num_str = regexprep(filename, 'Processed_\d+_(\d+)', '$1')
To read in the file contents, assuming that it's all comma-separated values without a header, use textscan, e.g.,
fid = fopen(filename)
C = textscan(fid, '%f,%f,%f') % Use as many %f specifiers as you have entries per line in the file
textscan also works on strings. So, for example, if your file contents was:
filestr = sprintf('1, 2, 3\n1, 3, 3')
Then running textscan on filestr works like this:
C = textscan(filestr, '%f,%f,%f')
C =
[2x1 int32] [2x1 int32] [2x1 int32]
You can convert that to a matrix using cell2mat:
cell2mat(C)
ans =
1 2 3
1 3 3
You could then repeat this procedure for all files with the same ID and concatenate them into a single matrix, e.g.,
C_full = [];
for (all files with the same ID)
C = do_all_the_above_stuff;
C_full = [C_full; C];
end
Then you can look for what you want in C_full.
Update based on updated OP Dec 12, 2013
Here's code to read the values from a single file. Wrap this all in the the loop that I mentioned above to loop over all your files and read them all into a single matrix.
fid = fopen('/path/to/file');
% Skip over 12 header lines
for kk = 1:12
fgetl(fid);
end
% Read in values to a matrix
C = textscan(fid, '%f,%f,%f');
C = cell2mat(C);
I think your requirements are too complicated to write the whole script here. Nonetheless, I will try to give some pointers to help. Disclaimer: None of this is tested, just my best guess. Please expect syntax errors, etc. I hope you can figure them out :-)
1) You can use the textscan function with the delimiter option to get data from the lines of your file. Since your format varies as it does, we will probably want to use...
2) ... fgetl to read the first two lines into strings and process them separately using texstscan. Such an operation might look like:
fid = fopen('file.txt','w');
tline1 = fgetl(fid);
tline2 = fgetl(fid);
fclose(fid);
C1 = textscan(tline1,'%s %d %s','delimiter','_'); %C1{2} will be the integer we want
C2 = textscan(tline2,'%s %s'),'delimiter,':'); %C2{2} will be the values we want, but they're still a string so...
mat = str2num(C2{2});
3) Then, for the rest of the lines, we can use something like dlmread:
mat2 = dlmread('file.txt',',',2,0);
The 2,0 specifies the offset in 0-based rows,columns from the start of the file. You may need to look at something like vertcat to stitch mat and mat2 together.
4) The list of files in the directory can be found with the dir command. The filename is an attribute of the structure that's returned:
dirlist = dir;
for i = 1:length(dirlist)
filename = dirlist(i).name
%process your files
end
You can also pass matching strings to dir, like so:
dirlist = dir('*.txt');
which will find all of the files with extension .txt.
5) You can very easily loop through the comparison matrix:
sze = size(comparisonmatrix);
for i = 1:sze(1)
%compare comparisonmatrix(i,1) to C1{2}
%Perform whatever operations you need
end
Hope that helps!

How can I display and access structure array contents in MATLAB?

Firstly, I have the user input their own text files consisting of states, capitals, and populations and I put all of these values into a structure array using the following code:
clear
clc
%Part A
textfile=input('What is the name of your text file?\n','s');
fid=fopen(textfile);
file=textscan(fid,'%s %s %f','delimiter',',');
State=file{1}
Capital=file{2}
Population=file{3}
regions=struct('State',State,...
'Capital',Capital,...
'Population',Population)
fclose(fid);
My first question: is it possible to display all of the values in the structure? Displaying the structure array just gives me this:
50x1 struct array with fields:
State
Capital
Population
And my second question: is it possible for me to access information in this structure by trying to find, for example, 'California' only?
As you've already discovered, the default display of structure arrays in MATLAB doesn't tell you much, just the array dimensions and field names. If you want to see the contents, you'll have to create formatted output yourself. One way you can do this is to use STRUCT2CELL to collect the structure contents in a cell array, then use FPRINTF to display the cell contents in a particular format. Here's an example:
>> regions = struct('State',{'New York'; 'Ohio'; 'North Carolina'},...
'Capital',{'Albany'; 'Columbus'; 'Raleigh'},...
'Population',{97856; 787033; 403892}); %# Sample structure
>> cellData = struct2cell(regions); %# A 3-by-3 cell array
>> fprintf('%15s (%s): %d\n',cellData{:}); %# Print the data
New York (Albany): 97856
Ohio (Columbus): 787033
North Carolina (Raleigh): 403892
With regard to your second question, you can collect the entries from the 'State' fields in a cell array, compare them to a given name with STRCMP to get a logical index, then get the corresponding structure array element:
>> stateNames = {regions.State}; %# A 1-by-3 cell array of names
>> stateIndex = strcmp(stateNames,'Ohio'); %# Find the index for `Ohio`
>> stateData = regions(stateIndex) %# Get the array element for `Ohio`
stateData =
State: 'Ohio'
Capital: 'Columbus'
Population: 787033
NOTE:
As you mention in a comment, each 'Population' entry in your structure array ends up containing the entire 50-by-1 vector of population data. This is likely due to the fact that file{3} in your sample code contains a vector, while file{1} and file{2} contain cell arrays. In order to properly distribute the contents of the vector in file{3} across the elements of the structure array, you need to break the vector up and place each value in a separate cell of a cell array using NUM2CELL before passing it to STRUCT. Defining Population like this should solve the problem:
Population = num2cell(file{3});

Resources