简体   繁体   English

Matlab使用字符串和浮点数生成二维图形

[英]Matlab using string and floats for 2d graph

Currently i have a function which plots the data in a graph as following: 目前,我有一个函数,可以在图表中绘制数据,如下所示:

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

However The lines currnetly generated give the name ch1,ch2 etc. I want to change that dynamically from the same file the data is coming from. 但是,currnetly生成的行的名称分别为ch1,ch2等。我想从数据来自的同一文件中动态更改该名称。 A small snippet from the data is here which is a .csv important via matlab: 这里是数据的一小段,这是通过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;

Im importing this all with a Matrix however i can't figure out how to use the strings "Chipset, HDD" etc for the names instead of ch1, ch2. 我用矩阵导入所有这一切,但是我不知道如何使用字符串“ Chipset,HDD”等代替ch1,ch2作为名称。

Ok, just import your data in a cellarray with text for field, as in the below image, 好的,只需将您的数据导入带有字段文本的单元格中,如下图所示,

在此处输入图片说明

and you will end up with: 然后您将得到:

>> 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'

then extract your labels for the legend: 然后为图例提取标签:

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

legends = 

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

Then you can get your data by, 然后,您可以通过以下方式获取数据:

>> 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'

which can be turned into a matrix of doubles by, 可以变成两倍的矩阵

>> 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

well, you can work from here... 好吧,你可以在这里工作...

OK, I've imported my data and called showg(file) , 好的,我已经导入了数据并称为showg(file)

your varargin will be a 1x1cell , so just do newfile = varargin{1} . 您的varargin将是1x1cell ,所以只需执行newfile = varargin{1}

Example of your function 功能实例

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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM