Matlab使用字符串和浮点数为2d图

时间:2015-06-23 13:20:06

标签: matlab

目前我有一个功能,可以在图表中绘制如下数据:

function [ dim ] = showg( varargin )
%SHOWG Summary of this function goes here
%   Detailed explanation goes here

linespec = {'b', 'g', 'r', 'c', 'm', 'y', 'k'};
figure
hold on
filter_size = 1000;
a=1;
b = (1/filter_size)*ones(1,filter_size);

x=1;
y=0;
while x<=nargin

ch = cell2mat(varargin(x+1));
input = cell2mat(varargin(x));    
    %ch = CInt(varargin(x+1))
    %input = varargin(x)
    dim = size(input);

    for i = 2:(ch+1)
        z=i+y-1;
        color = mod(z,7);
        if color == 0
            color = 7;
        end
        out = (input(51:dim(1),1)-input(51,1))/1000;
        out2 = filter(b,a,input(51:dim(1),i));
        size(out)
        size(out2)
        plot(out',out2,linespec{color});
        legendInfo{z} = ['ch' num2str(z)];
    end
    y=(i-1);


    x=x+2;

end
legend(legendInfo);
xlabel('Time in s');
ylabel('Energy in J');

title('new plot')

   hold off; 
return 
end

然而,currnetly生成的行给出了名称ch1,ch2等。我想从数据来自的同一文件中动态地更改它。数据的一小段在这里是通过matlab重要的.csv:

;Chipset;HDD;CPU1;MEM1;
43445653;0.01;0.01;0.01;0.00;
43445654;0.02;0.01;0.01;0.01;
43445655;0.03;0.02;0.02;0.01;
43445656;0.04;0.02;0.03;0.02;

我用矩阵导入这一切然而我无法弄清楚如何使用字符串“Chipset,HDD”等代替ch1,ch2。

1 个答案:

答案 0 :(得分:2)

好的,只需在带有文字字段的cellarray中导入数据,如下图所示,

enter image description here

你将最终得到:

>> file

file = 

    ''            'Chipset'    'HDD'     'CPU1'    'MEM1'
    '43445653'    '0.01'       '0.01'    '0.01'    '0.00'
    '43445654'    '0.02'       '0.01'    '0.01'    '0.01'
    '43445655'    '0.03'       '0.02'    '0.02'    '0.01'
    '43445656'    '0.04'       '0.02'    '0.03'    '0.02'

然后提取图例的标签:

>> legends = file(1,2:end)

legends = 

    'Chipset'    'HDD'    'CPU1'    'MEM1'

然后您可以通过

获取数据
>> data = file(2:end, :)

data = 

    '43445653'    '0.01'    '0.01'    '0.01'    '0.00'
    '43445654'    '0.02'    '0.01'    '0.01'    '0.01'
    '43445655'    '0.03'    '0.02'    '0.02'    '0.01'
    '43445656'    '0.04'    '0.02'    '0.03'    '0.02'

可以转换为双打矩阵,

>> data = cell2mat(cellfun(@str2num,data,'un',0))

data =

    43445653    0,01    0,01    0,01    0
    43445654    0,02    0,01    0,01    0,01
    43445655    0,03    0,02    0,02    0,01
    43445656    0,04    0,02    0,03    0,02
嗯,你可以在这里工作......

好的,我已导入了我的数据,并调用了showg(file)

您的varargin将是1x1cell,所以只需newfile = varargin{1}

您的功能示例

function [ dim ] = showg( varargin )
%SHOWG Summary of this function goes here
%   Detailed explanation goes here

%% MYCODE
newfile = varargin{1};
legends = newfile(1,2:end);
data = newfile(2:end, :);
data = cell2mat(cellfun(@str2num,data,'un',0));

%% go from here...