function Q_index0=Question global Data_Real global GUI RangeFlag=get(GUI.RangeSelect,'value'); WordFlag=Data_Real(:,3); if RangeFlag<=2 b=1:size(Data_Real,1); b=b'; elseif RangeFlag==3 a=cellfun(@isnan,WordFlag); b=find(a==1); elseif RangeFlag==4 a=cellfun(@isnan,WordFlag); b=find(a==0); c=WordFlag(b,1); c=cell2mat(c); c=find(c>0); b=b(c,1); end if isempty(b)==0 for i=1:10 value=rand(1,1); value=value*size(b,1); value=round(value); value=min(value,size(b,1)); value=max(value,1); Q_index0(i,1)=b(value,1); b(value,:)=[]; if isempty(b)==1 break end end else Q_index0=[]; end end
时间: 2024-04-13 22:27:42 浏览: 15
这段代码是一个函数,名为Question。函数中使用了两个全局变量Data_Real和GUI。首先,它获取GUI界面中的RangeSelect的值,并将其存储在RangeFlag变量中。接下来,它根据RangeFlag的值执行不同的操作。
如果RangeFlag小于等于2,那么b将被设置为从1到Data_Real行数的向量。如果RangeFlag等于3,那么会使用cellfun函数检查WordFlag中的NaN值,并将其索引存储在向量a中。然后,将a中NaN值的索引存储在向量b中。如果RangeFlag等于4,那么同样会使用cellfun函数检查WordFlag中的NaN值,并将其索引存储在向量a中。然后,将a中非NaN值的索引存储在向量b中,并将WordFlag中对应索引的值存储在向量c中。接着,将c中大于0的索引存储在向量c中,并将b中对应索引的值存储在向量b中。
接下来,如果b不为空,那么会执行一个循环10次的操作。在每次循环中,会生成一个随机数value,并将其乘以b的长度。然后将value四舍五入为整数,并确保其在1和b的长度之间。将b中对应索引的值存储在Q_index0的第i行第1列,并从b中移除该值。如果b为空,则跳出循环。
最后,如果b为空,那么Q_index0将被设置为空向量。函数执行完毕后,Q_index0将被返回。
相关问题
这段代码报错 void function return value is used。请告诉我如何修改:var start:string = document.getElementById("getstart").addEventListener("click", function():string { select_bu01 = 0 select_bu02 = 0 select_bu03 = 0 select_bu04 = 0 select_bu05 = 0 select_y01 = 0 select_y02 = 0 select_y03 = 0 select_y04 = 0 select_y05 = 0 select_y06 = 0 select_y07 = 0 select_y08 = 0 select_y09 = 0 select_y10 = 0 select_h01 = 0 select_h02 = 0 ifMDM = 0 let tmp_data = { MDM: ifMDM, BU01: select_bu01, BU02: select_bu02, BU03: select_bu03, BU04: select_bu04, BU05: select_bu05, Year1: select_y01, Year2: select_y01, Year3: select_y01, Year4: select_y01, Year5: select_y01, Year6: select_y01, Year7: select_y01, Year8: select_y01, Year9: select_y01, Year10: select_y01 } let jsonString = JSON.stringify(tmp_data); console.log(jsonString); });
这段代码的问题在于你将一个返回类型为string的匿名函数赋值给了addEventListener方法。然而,addEventListener方法期望传入一个没有返回值的函数作为参数。
要修复这个问题,你可以将函数的返回类型声明为void,而不是string。这样,就不会再报错"void function return value is used"了。修改后的代码如下:
```typescript
var start: string = document.getElementById("getstart").addEventListener("click", function(): void {
select_bu01 = 0;
select_bu02 = 0;
select_bu03 = 0;
select_bu04 = 0;
select_bu05 = 0;
select_y01 = 0;
select_y02 = 0;
select_y03 = 0;
select_y04 = 0;
select_y05 = 0;
select_y06 = 0;
select_y07 = 0;
select_y08 = 0;
select_y09 = 0;
select_y10 = 0;
select_h01 = 0;
select_h02 = 0;
ifMDM = 0;
let tmp_data = {
MDM: ifMDM,
BU01: select_bu01,
BU02: select_bu02,
BU03: select_bu03,
BU04: select_bu04,
BU05: select_bu05,
Year1: select_y01,
Year2: select_y01,
Year3: select_y01,
Year4: select_y01,
Year5: select_y01,
Year6: select_y01,
Year7: select_y01,
Year8: select_y01,
Year9: select_y01,
Year10: select_y01
};
let jsonString = JSON.stringify(tmp_data);
console.log(jsonString);
});
```
现在,你应该不再遇到"void function return value is used"的错误了。
代码报错 需要找下原因 // 初始化 const start = document.getElementById("getstart").addEventListener("click", function () { let select_bu01 = 0; select_bu02 = 0; select_bu03 = 0; select_bu04 = 0; select_bu05 = 0; select_y01 = 0 select_y02 = 0 select_y03 = 0 select_y04 = 0 select_y05 = 0 select_y06 = 0 select_y07 = 0 select_y08 = 0 select_y09 = 0 select_y10 = 0 select_h01 = 0 select_h02 = 0 ifMDM = 0 let tmp_data = { MDM: ifMDM, BU01: select_bu01, BU02: select_bu02, BU03: select_bu03, BU04: select_bu04, BU05: select_bu05, Year1: select_y01, Year2: select_y02, Year3: select_y03, Year4: select_y04, Year5: select_y05, Year6: select_y06, Year7: select_y07, Year8: select_y08, Year9: select_y09, Year10: select_y10 }; let jsonString = JSON.stringify(tmp_data); console.log(jsonString); return jsonString; });
这段代码中的问题在于,你在点击事件的处理函数中使用了 `return` 语句来返回 `jsonString`。然而,`addEventListener` 方法并不期望处理函数返回值,所以无法正确处理这个返回值。
如果你想在点击事件处理函数执行完毕后获取 `jsonString` 的值,可以考虑将其保存到外部变量或者调用另一个函数来处理。
下面是修改后的代码示例:
```javascript
const start = document.getElementById("getstart").addEventListener("click", function () {
let select_bu01 = 0;
let select_bu02 = 0;
let select_bu03 = 0;
let select_bu04 = 0;
let select_bu05 = 0;
let select_y01 = 0;
let select_y02 = 0;
let select_y03 = 0;
let select_y04 = 0;
let select_y05 = 0;
let select_y06 = 0;
let select_y07 = 0;
let select_y08 = 0;
let select_y09 = 0;
let select_y10 = 0;
let select_h01 = 0;
let select_h02 = 0;
let ifMDM = 0;
let tmp_data = {
MDM: ifMDM,
BU01: select_bu01,
BU02: select_bu02,
BU03: select_bu03,
BU04: select_bu04,
BU05: select_bu05,
Year1: select_y01,
Year2: select_y02,
Year3: select_y03,
Year4: select_y04,
Year5: select_y05,
Year6: select_y06,
Year7: select_y07,
Year8: select_y08,
Year9: select_y09,
Year10: select_y10
};
let jsonString = JSON.stringify(tmp_data);
console.log(jsonString);
// 在这里可以使用 jsonString 或者调用其他函数来处理它
});
```
在上述示例中,我移除了 `return` 语句,并将 `jsonString` 的值保留在了点击事件处理函数的内部。你可以在 `console.log` 后面的注释处使用 `jsonString` 或者调用其他函数来处理它。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)