从Excel文件读取和操作数据

我使用Matlab从Excel文件读取。 我应该在阅读一个名为Change Due的整数列。 读完专栏之后,我应该计算这个变化,在Quarters,Dimes ..等等

我可以阅读专栏没有问题,我理解如何计算变化的math。 然而使用条件来检查更改的数量是困难的… … –

问题:

我知道对于vector的乘法/除法,你可以添加一个句点来使它成为一个标量。*或者./

但是,我如何通过一个条件parsingvector?

if(Change.<25&&Change.>=10) 

我得到这个错误:

 Operands to the || and && operators must be convertible to logical scalar values. 

如果我只是简单的离开这段时间,我不会得到一个错误消息,但它只是通过计算的第一行。

码:

 % Filename: Program_04_1 % Author: Stewart Moon % Assisted by: No one % Program Description: % The purpose of this program is to demonstrate how to read from an Excel % file and to then calculate the amount of coins it will take per row clc % clc clears the contents of the command window clear % clear, clears all defined variables form the Matlab workspace close all % closes all figure windows % Declare Variables Quarters=0; Dimes=0; Nickels=0; Pennies=0; TotalCoins=0; % Output of the title and author to the command window fprintf('Output for Program_04_1 written by Stewart Moon.\n') fprintf('\nOriginal Data read from Program_04_1_Data.xlsx\n') fprintf('\nMinimum Number of Coins Needed to Make Change\n') Change=xlsread('Tutorial_04_1_Data.xlsx','Coins','B4:B43'); % Reading the column in Excel and storing it the variable Change % Output Header Format fprintf('\nChangeDue(cents) Quarters Dimes Nickels Pennies Total Coins\n\n') table=[Change]; disp([table]) 

命令窗口输出:

 Output for Program_04_1 written by Stewart Moon. Original Data read from Program_04_1_Data.xlsx Minimum Number of Coins Needed to Make Change ChangeDue(cents) Quarters Dimes Nickels Pennies Total Coins 1 4 5 6 10 14 15 16 20 24 25 29 30 31 35 37 40 42 45 49 50 54 55 56 60 64 65 68 70 73 75 77 80 81 85 88 90 91 95 99 

计算变更伪码:

 while(Change>0) if (Change>=25) Change=Change-25; TotalCoins=TotalCoins+1; Quarters=Quarters+1; end if (Change<25&&Change>=10) Change=Change-10; TotalCoins=TotalCoins+1; Dimes=Dimes+1; end if (Change<10&&Change>=5) Change=Change-5; TotalCoins=TotalCoins+1; Nickels=Nickels+1; end if (Change<5&&Change>=1) Change=Change-1; TotalCoins=TotalCoins+1; Pennies=Pennies+1; end end 

从我的理解,你想要达到像这样的东西:

 for i = 1 : length(Change) % your conditions using indices for the array % example: % if(Change(i) > 25) % instructions % end end