How do I convert this to a while loop?

5 ビュー (過去 30 日間)
David Hughes
David Hughes 2015 年 8 月 2 日
コメント済み: David Hughes 2015 年 8 月 3 日
I have a script that works but I would like to convert it to a while loop and I am stuck. Any help is appreciated. Script is below. The script reads a text file that is input by the user. I can send the text file for testing if needed.
clc% Clear the command window.
clear % Clears the workspace to prevent issues with other stored information
%The next line prompts the user to enter a file name.
%The input expects a string so the user does not have to use single quotes around the file name.
x = input('Enter file name: ','s');
%Reading the information in the text file and storing it as a string.
textread(x,'%s')%Charcters between spaces will stored as 1 string
%Storing the information from x to the variable srt1
str1 = textread(x,'%s');
% Variable to be used for storing the added value of each decoded row in text file
sum = 0;
%aleph = 1;beit = 2;gimel = 3;dalet = 4;hei = 5;vav = 6;zayin = 7;cheit = 8;
%tet = 9;yud = 10;khaf = 20;lamed = 30;mem = 40;nun = 50;samech = 60;
%ayin = 70;pei = 80;tzadi = 90;quf = 100;resh = 200;shin = 300;tav = 400;
k=1; % Variable to be used with resulting vector
vecsum = 0;%This will be the resulting vector
for i=1:length(str1)
if strcmp(str1{i},'aleph')
a = 1;sum = sum + a;
elseif strcmp(str1{i},'beit')
a = 2;sum = sum + a;
elseif strcmp(str1{i},'gimel')
a = 3;sum = sum + a;
elseif strcmp(str1{i},'dalet')
a = 4;sum = sum + a;
elseif strcmp(str1{i},'hei')
a = 5;sum = sum + a;
elseif strcmp(str1{i},'vav')
a = 6;sum = sum + a;
elseif strcmp(str1{i},'zayin')
a = 7;sum = sum + a;
elseif strcmp(str1{i},'cheit')
a = 8;sum = sum + a;
elseif strcmp(str1{i},'tet')
a = 9;sum = sum + a;
elseif strcmp(str1{i},'yud')
a = 10;sum = sum + a;
elseif strcmp(str1{i},'khaf')
a = 20;sum = sum + a;
elseif strcmp(str1{i},'lamed')
a = 30;sum = sum + a;
elseif strcmp(str1{i},'mem')
a = 40;sum = sum + a;
elseif strcmp(str1{i},'nun')
a = 50;sum = sum + a;
elseif strcmp(str1{i},'samech')
a = 60;sum = sum + a;
elseif strcmp(str1{i},'ayin')
a = 70;sum = sum + a;
elseif strcmp(str1{i},'pei')
a = 80;sum = sum + a;
elseif strcmp(str1{i},'tzadi')
a = 90;sum = sum + a;
elseif strcmp(str1{i},'quf')
a = 100;sum = sum + a;
elseif strcmp(str1{i},'resh')
a = 200;sum = sum + a;
elseif strcmp(str1{i},'shin')
a = 300;sum = sum + a;
elseif strcmp(str1{i},'tav')
a = 400;sum = sum + a;
elseif strcmp(str1{i},'-1')
continue
elseif strcmp(str1{i},'0')
fprintf('%i \n',sum)% This line moves the prompt 1 line down
vecsum(k,1) = sum;% This stores the values into a vector
k = k + 1;
sum = 0;
end
end
%This code will ask the user for the name of the excel file being created.
%Same as above the input is expected to be a string no single quotes needed
str = input('Name the excel file: ','s');
%This command creates the excel file and uses the string stored to str
xlswrite(str,vecsum)

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 2 日
You should be considering using ismember() to return the index of str1 in a cell array of strings that are the possibilities. Use the returned string index to index an array of corresponding values [1:9 10:10:90 100:100:400 0 nan]. Break the vector up at the locations where the '0' are detected, and return the sum() over each segment.
  1 件のコメント
David Hughes
David Hughes 2015 年 8 月 3 日
This script was written to achieve the result the way the instructor wanted it. I was just curious if it could be written as a while loop.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by