简体   繁体   English

MATLAB元胞数组索引和循环

[英]MATLAB cell array indexing and looping

I'm trying to create a script that reads data from a text file, and plots the data onto a scatter plot. 我正在尝试创建一个脚本,该脚本从文本文件中读取数据,并将数据绘制到散点图上。

For example, say the file name is prices.txt and contains: 例如,假设文件名为prices.txt,并且包含:

Pens 2 4
Pencils 1.5 3
Rulers 3 3.5 
Sharpeners 1 3
Highlighters 3 4

Where columns 2 and 3 are prices of the items for two different stores. 其中第2列和第3列是两家不同商店的商品价格。

What my script should do is read the prices, calculates (using another function) future prices of the stores and plots these prices onto a scatter plot where x is one store and y is another. 我的脚本应该做的是读取价格,(使用另一个函数)计算商店的未来价格,并将这些价格绘制到一个散点图上,其中x是一家商店,y是另一家商店。 This is a silly example I know but it fits the description. 我知道这是一个愚蠢的示例,但符合说明。

Don't worry to much about the other function that does the calculation, just assume it does what its supposed to. 不必担心其他执行计算的函数,只需假设它执行了预期的操作即可。

Basically, I've come up with the following: 基本上,我提出了以下内容:

pricesfile = fopen('Prices.txt');
prices = textscan(pricesfile, '%s %d d');
fclose(pricesfile);

count = 1;
while count <= length(prices{1})
    for item = constants{1}
        name = constants{1}{count};
        store_A = prices{2}{count};
        store_B = prices{3}{count};
        (...other function goes here...)
    end
end

After doing this I'm completely stuck. 完成此操作后,我完全被卡住了。 My thought process behind this was to go through each item name, and create a vector that's assigned to this name with its two corresponding prices as items in the vector eg: 我对此的思考过程是遍历每个商品名称,并创建一个分配给该名称的矢量,其两个对应的价格作为矢量中的商品,例如:

pens = [2 4]
pencils = [1.5 3]

etc. Then, I would somehow plot those items in the vector on a scatter plot and use the name of the vector as a label. 然后,我将以某种方式在散点图上的矢量中绘制这些项目,并使用矢量的名称作为标签。

I'm not too sure how to carry out the rest of my code or even if what I've written will get me to the solution. 我不太确定如何执行其余的代码,或者即使我写的内容能使我找到解决方案。

Please help and thanks in advance. 请帮助并提前致谢。

pricesfile = fopen('Prices.txt');
data = textscan(pricesfile, '%s %d d');
fclose(pricesfile);

You were on the right track but after this (through a bit of hackery) you don't actually need a loop: 您的方向是正确的,但是在此之后(通过一些黑客手段),您实际上不需要循环:

plot(repmat(data{2},1,2)', repmat(data{3},1,2)', '.')
legend(data{1})

What you DO NOT want to do is create variables named after strings. 您不想做的是创建以字符串命名的变量。 Rather store them in an array with an array of the names (which is basically what your textscan code gives you). 而是将它们存储在具有名称数组的数组中(这基本上是您的textscan代码为您提供的内容)。 Matlab is very good at handling matrices/arrays. Matlab非常擅长处理矩阵/数组。

You could also split your price array up for example: 您还可以拆分price数组,例如:

names = prices{1};
prices = [data{2:3}];

now you can perform calculations on prices quite easily like prices_cents = prices*100; 现在您可以很容易地对价格进行计算,例如prices_cents = prices * 100;

plot(prices_cents(:,[1,1]), prices_cents(:,[2,2]))
legend(names)

Note that the [1,1] etc above is just using indexing as a short hand to achieve what repmat does... 请注意,上面的[1,1]等只是使用索引作为简写来实现repmat功能...

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

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