Solved

creating variables with colheaders after Matlab importdata

Posted on 2010-09-18
2
1,571 Views
Last Modified: 2016-03-02
Hi,

After importing a tab delimited file into matlab using the importdata function i get my headers in one cell array (textdata or colheaders) and the numerical data in data matrix.

I want to create variables from the first four letters of  each column header with the corresponding data in the data matrix. Is is possible. Please help!

Thanks
Salamay
0
Comment
Question by:salamay
2 Comments
 
LVL 1

Author Comment

by:salamay
ID: 33709367
this is how i did it and it worked

check this link for more info http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
varnames = colheaders;
for i=1:length(colheaders)
    s.(varnames{i})= data(:,i);
end

Open in new window

0
 
LVL 15

Accepted Solution

by:
yuk99 earned 300 total points
ID: 33711134
Since you want to use only first 4 letters I modified the salamay's code.
% replace invalid characters by underscore
varnames = regexprep(colheaders,'\W','_');
% convert cellarrays to char
varnames = char(varnames);
% get first 4 letters
varnames = varnames(:,1:4);
% create structure with dynamic fields
for k=1:size(varnames,1)
    % strtrim to remove leading and trailing spaces
    S.(strtrim(varnames(k,:))) = data(:,k);
end

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question