mGm
mGm

Reputation: 262

Data format from Matlab workspace

I have a data stored in the "Matlab workspace" in the following format:

data = 
Columns 1 through 12

-1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1

and I want to convert it into this simple form:

-1-1-1-1-1-1-1-1-1-1-1-1

or even in this form also acceptable:

-1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1

the output data intended to be equal to the result of this command:

Str = ('-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1');

originally I have a data in the following form "type=Double":

-1
-1
-1
-1
-1
-1
-1
-1
-1

I save this type double data into a variable using following command:

save data.txt data

Then I use the following code to import this data in my .mat file for further operation:

str = uiimport(); % import data
str = sprintf('%g', str); % or mat2str(str)

the resulting data type from this is although type char which is my required data type to get it process further. But it does not work. However if I directly put this data in my .mat file as str = ('-1 -1 -1 -1 - 1'); it works.

Upvotes: 0

Views: 184

Answers (1)

Matt
Matt

Reputation: 13923

Cause

I had a hard time to figure out why you mention that num2str or sprintf don't work. I think you tried to directly pass the output of uiimport to num2str or sprintf. This gives you the error below. It would have been very helpful to include this in your question.

Error using sprintf
Function is not defined for 'struct' inputs.

Solution

The output of uiimport is a struct with all the resulting variables as fields. Therefore you need to address the data-field inside this struct like this: S.data.

Here is the code:

data = [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]'

save data.txt data              % store data
S = uiimport();                 % import data

str = sprintf('%g', S.data)     % without spaces
str = sprintf('%g ', S.data)    % with spaces

This is the result:

data =
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
str =
-1-1-1-1-1-1-1-1-1-1-1-1
str =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Upvotes: 2

Related Questions