Errors with audiorecord function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello everyone, I've written the below code so far.

Need Matlab to record(function to be running/active) for as long the button is pressed.
Then use the above running time as an input for the record function (screenshot).
Imagine it being like a play/pause button.
Gives me some errors
Can I use while (as long the state button value is True) instead of if(skips some code parts)?
Thanks in advance
1 件のコメント
Nikos Korobos
2022 年 3 月 18 日
Is it possible to replace the if with a while?
採用された回答
Walter Roberson
2022 年 3 月 18 日
Yes, it is possible. However, you cannot achieve your goals by doing that.
You are in the middle of a callback that fires every time the state of the button changes -- which can mean that it was pressed to start or can mean that it was pressed to end.
You should have your app store the current status, currently recording or not. You should have your app store the recorder object (at the app level).
When the button is pushed, have the value changed function check to see if it is a request to start recording or to stop recording.
- If it is a request to start recording and you are already recording, either discard the press or else stop the existing recording and continue on as if you were not already recording.
- If it is a request to start recording and you are not already recording (or you "fell through" after stopping an existing recording), create the recorder object and store it at the app level, and set your state to "recording", activate the record() method of the recorder, and return from the callback.
- If it is a request to stop recording and you are not already recording, discard the press
- If it is a request to stop recording and you are currently recording, stop the recording, grab the data, remove the recorder object, set your state to not-recording, and enable any actions that should not be possible to perform until you have recorded data, and return
When you activate the record() method, I recommend that you put in a maximum length, to prevent it from overflowing the memory.
13 件のコメント
Nikos Korobos
2022 年 3 月 19 日
Thanks for your input so far, its a state button the one im currently using here.
Nikos Korobos
2022 年 3 月 19 日
Also what do you mean by:
You should have your app store the current status, currently recording or not. You should have your app store the recorder object (at the app level).
Walter Roberson
2022 年 3 月 19 日
app.currently_recording = true;
app.recorder = audiorecorder(fs, bits, ch);
Nikos Korobos
2022 年 3 月 20 日
Got one more question.
Should app.currently_recording have the value of app.RecordButton.Value or not?
If you could explain a bit more what you sent above it would be great.
Seems like the recording starts by itself with the code above.
Thanks in advance
Nikos Korobos
2022 年 3 月 20 日
I would like the recording to start when the value of the button is equal to 1 or True
Walter Roberson
2022 年 3 月 20 日
Assume that somehow the program might have lost track of events or receive them out of order. You might receive two callbacks to Start in a row, or two callbacks to Stop in a row. It is not good programming practice to assume that the events generated by the user interface perfectly match the program state.
It is better for the program to keep track of whether it believes that it is currently recording or not. When it receives a Start event it should check its own memory of what it is doing in case it got a Start while it is already started. Likewise if it got a Stop event it should check its memory of what it thinks it is doing in case it is already stopped.
Walter Roberson
2022 年 3 月 20 日
Your current code tests if the value is "True". However uibutton style state has value 0 or 1. Instead of
if value == "True"
code
if value
Nikos Korobos
2022 年 3 月 20 日
I get what you 're saying, but isn't this condition supposed to get values such as 1 or 0?
Walter Roberson
2022 年 3 月 20 日
In MATLAB the test
if Value
is equivalent to
if all(logical(Value(:)) == true) == true
To phrase that a different way:
- the Value must be convertible to numeric
- the converted value must not be NaN
- all converted values must be non-zero
So even a single zero leads to the test being considered to fail, and NaN leads to error, and otherwise the condition is considered to succeed
So you do not need to explicitly
if value == 1
What you care about is that it is non-zero and just naming the variable without a relation operator automatically tests for non-zero
Nikos Korobos
2022 年 3 月 21 日
編集済み: Nikos Korobos
2022 年 3 月 21 日
I think i got a grip on what you are saying.
Would you point out any mistakes in my code (according to your directions)?
Sorry for spamming but I'm a total beginner on Matlab.
Thanks in advance

Walter Roberson
2022 年 3 月 21 日
Do not do the
app.currently_recording = true;
at that point. At the place you initialize your other variables you should initialize it to false and leave it like that until you are switching into recording mode.
In the case for
if value & app.currently_recording == false
you have
app.currently_recording == true;
but that should be
app.currently_recording = true;
Then in the "if it is a request to stop" case, you currently have
if value & app.currently_recording = true
and you should change that to
if ~value & app.currently_recording = true
and change the
app.currently_recording == false;
to
app.currently_recording = false;
Remember that == is comparison, and = is assignment.
Nikos Korobos
2022 年 3 月 22 日
Hello again, so I've done some testing with your suggestions and my code is currently like this.

Although it does run and the Record button wont give me any errors, it doesn't seem to record anything.
Any ideas?
Thanks in advance
Nikos Korobos
2022 年 3 月 22 日
I have also added a Properties section

その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Whos についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
