Taking a string input and transferring it to a integer

I am trying to write a script to calculate the nominal kohms of a resistor. I need to create a menu of 10 colors and be able to assign those colors a number that I can use to create the final equasion. The numbers are integers ranging from 0 to 9 and there are three user selected inputs. Thanks in advance!

1 件のコメント

DGM
DGM 2021 年 10 月 14 日
If you really insist on doing this with dialogs, you can use listdlg.
Otherwise, build a uifigure or something

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

 採用された回答

DGM
DGM 2021 年 10 月 14 日
編集済み: DGM 2021 年 10 月 15 日

1 投票

Meh.
numbands = 4;
colornames = {'black','brown','red','orange','yellow','green','blue','violet','gray','white','gold','silver'};
tolerancemap = [1 2 0.5 0.25 0.1 0.05 5 1]; % in percent
multmap = [0:7 -1 -2];
digits = zeros(1,numbands-2);
for vb = 1:numbands
ps = sprintf('Color Band %d',vb);
if vb == numbands
%tol = listdlg('promptstring',ps,'selectionmode','single','liststring',colornames([2 3 6:9 11 12]));
tol = menu(ps,colornames([2 3 6:9 11 12]));
elseif vb == numbands-1
%multiplier = listdlg('promptstring',ps,'selectionmode','single','liststring',colornames([1:8 11 12]));
multiplier = menu(ps,colornames([1:8 11 12]));
else
%digits(vb) = listdlg('promptstring',ps,'selectionmode','single','liststring',colornames);
digits(vb) = menu(ps,colornames);
end
end
value = str2double(sprintf('%d',digits-1)) * 10^multmap(multiplier);
tol = tolerancemap(tol);
if value >= 1E6
fprintf('The resistor value is %.2f Mohms, with a tolerance of %.2f%%\n',value/1E6,tol)
elseif value >= 1E3
fprintf('The resistor value is %.2f kohms, with a tolerance of %.2f%%\n',value/1E3,tol)
else
fprintf('The resistor value is %.2f ohms, with a tolerance of %.2f%%\n',value,tol)
end
Now you can add the tempco band too.

5 件のコメント

Jacob Belouin
Jacob Belouin 2021 年 10 月 15 日
Thanks for the answer!
I am a first time matlab user, I can understand a lot of these commands but i'm a little hung up on the first for statement and what that is actually doing. Specifically the zeros command and the elipsis in the listdlg commands.
thanks
DGM
DGM 2021 年 10 月 15 日
Here, zeros() is used to preallocate a vector to hold the significant digits of the value. I'm assuming the color code scheme uses one band for tolerance and one band for a value multiplier; hence, the length of this vector is 2 less than the number of color bands.
The ellipsis is just a way to break long expressions across multiple lines so that the parser knows it's all one line of code.
Jacob Belouin
Jacob Belouin 2021 年 10 月 15 日
Thanks again, huge help.
Jacob Belouin
Jacob Belouin 2021 年 10 月 15 日
Understanding how you approached this now, its a way more efficent method but my Professor is telling me it has to use the menu command rather than listdlg. I dont need the whole answer, but is there a way to index arrays within the menu command?
DGM
DGM 2021 年 10 月 15 日
Augh. I didn't even think about literally using menu(). I never use interactive stuff, so i don't remember any of these things.
I edited to add example usage of menu in place of listdlg(). The indexing is basically the same.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2021b

質問済み:

2021 年 10 月 14 日

コメント済み:

DGM
2021 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by