How to write output data in excel csv files?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
My for loops are as follows;
number of image files are 114
number of shape files are 242
for m = 1:length(imgfiles)
for n = 1:length(shapefiles)
avg_village_ndvi(n,m) = mean(village_ndvi(mask));
avge_village_ndre1(n,m) = mean(village_ndre1(mask));
avg_village_cire(n,m) = mean(village_cire(mask));
CAF(n,m) = nnz(village_ndvi(mask) > 0.4) / (size(village_ndvi,1)*size(village_ndvi,2))
end
end
I want to write these data in seperate excel csv file with file name as 'c:\data\ndvi_predictors.csv','c:\data\ndre1_predictors.csv','c:\data\cire_predictors.csv' and 'c:\data\caf_predictors.csv'.
I request all to please suggest me how to do it in above mentioned matlab code.
I would appreciate any help in this regard.
Dave
採用された回答
Voss
2024 年 3 月 25 日
After the for loops:
writematrix(avg_village_ndvi,'c:\data\ndvi_predictors.csv')
writematrix(avge_village_ndre1,'c:\data\ndre1_predictors.csv')
writematrix(avg_village_cire,'c:\data\cire_predictors.csv')
writematrix(CAF,'c:\data\caf_predictors.csv')
23 件のコメント
Devendra
2024 年 3 月 25 日
Thank you so much for helping me out in writing output data into csv files. One more thing I want to add the first column as name of shape file followed by data in successive columns of each csv output file. How to do it please suggest me this also.
Once again thank you very much for your kind support.
Dave
You're welcome!
Here's how you can include the shape files' names as the first column:
names = cellstr(shapefiles(:));
writecell([names num2cell(avg_village_ndvi)],'c:\data\ndvi_predictors.csv');
writecell([names num2cell(avge_village_ndre1)],'c:\data\ndre1_predictors.csv');
writecell([names num2cell(avg_village_cire)],'c:\data\cire_predictors.csv');
writecell([names num2cell(CAF)],'c:\data\caf_predictors.csv');
If this answer solved the problem, please "Accept" it. Thanks!
Devendra
2024 年 3 月 25 日
I am very grateful to you for helping me out. Just one more additional thing I want to add. suppose I want to extract date stamp from image file names and add to first row of each csv file. For example name of image file is as follows
20240314_MAWANA-SUBSET
I want to extract the date stamp as 20240314 from the first image file and so on. These date stamps I want to write in first row of each csv file. How to write these date stamps in first row of each csv file.
I really appreciate your kind cooperation.
Dave
[~,fn] = fileparts(imgfiles);
img_names = regexp(fn,'\d+','match','once');
img_names = cellstr(reshape(img_names,1,[]));
names = cellstr(shapefiles(:));
writecell([{''} img_names; names num2cell(avg_village_ndvi)],'c:\data\ndvi_predictors.csv');
writecell([{''} img_names; names num2cell(avge_village_ndre1)],'c:\data\ndre1_predictors.csv');
writecell([{''} img_names; names num2cell(avg_village_cire)],'c:\data\cire_predictors.csv');
writecell([{''} img_names; names num2cell(CAF)],'c:\data\caf_predictors.csv');
Thank you very much for your kind help. I have encountered a small problem as follows
Error using fileparts (line 15)
Input must be a row vector of characters, or a string scalar, or a cellstr, or a string matrix.
Error in ds_indices_caf (line 91)
[~,fn] = fileparts(imgfiles);
Please suggest me how to fix it.
Deva
That error is due to imgfiles being of an unexpected class. What class is imgfiles? To find out, please put the following just before the fileparts line, and show its output:
class(imgfiles)
class(imgfiles)
ans = 'struct'
Please have a look at it and suggest me to fix it.
deva
Voss
2024 年 3 月 30 日
I assume imgfiles is the struct returned from a dir() call. In that case, change the fileparts line to:
fn = {imgfiles.name};
If shapefiles is also a struct returned by dir(), then you'll have to modify the line defining names as well:
names = {shapefiles.name}.';
It worked successfully. I am very much thankful to you.
Deva
Voss
2024 年 3 月 30 日
You're welcome! Glad to hear it worked!
Devendra
2024 年 4 月 18 日
I want to write each line(row) two times in the following excel csv file. I request you to please suggest me how to do it in the following files
writecell([{''} img_names; names
num2cell(avg_village_ndvi)],'c:\data\ndvi_predictors.csv');
writecell([{''} img_names; names num2cell(avge_village_ndre1)],'c:\data\ndre1_predictors.csv');
writecell([{''} img_names; names num2cell(avg_village_cire)],'c:\data\cire_predictors.csv');
writecell([{''} img_names; names num2cell(CAF)],'c:\data\caf_predictors.csv');
Devendra
Voss
2024 年 4 月 18 日
C = [{''} img_names; names num2cell(avg_village_ndvi)];
writecell(repelem(C,2,1),'c:\data\ndvi_predictors.csv');
C = [{''} img_names; names num2cell(avge_village_ndre1)];
writecell(repelem(C,2,1),'c:\data\ndre1_predictors.csv');
% and so on for the others
Devendra
2024 年 4 月 19 日
Thanks for your kind help. It worked well.
Devendra
Voss
2024 年 4 月 19 日
You're welcome!
Devendra
2024 年 4 月 21 日
I want to use
fn = {imgfiles.name};
img_names = regexp(fn,'\d+','match','once');
img_names = cellstr(reshape(img_names,1,[]));
and want to extract the date stamp from the following file name
T43RGN_20210203T053029_B04_10m.jp2
as 20210203T053029
please suggest me how to get it using above mentioned lines.
Thanks for your kind help.
Devendra
img_names = regexp(fn,'_([\dT]+)_','tokens','once');
img_names = [img_names{:}];
Devendra
2024 年 4 月 21 日
Thank you so much for your help. Devendra
Voss
2024 年 4 月 21 日
You're welcome!
Thanks for your kind help and support. I want to add five data sets column wise before writing into excel file. For example each data has dimension of 18x24 after adding column wise dimension of new data will be18x120. So I will be writing new data set into single excel file in place of five different excel files. Please suggest me how to do it using matlab.
I will appreciate your kind cooperation.
Gauri
Voss
2024 年 5 月 7 日
all_data = [data1,data2,data3,data4,data5];
Then write all_data to file using an appropriate function (writematrix, writetable, writecell).
gauri
2024 年 5 月 7 日
Thank you very much for your kind help.
Voss
2024 年 5 月 7 日
You're welcome!
Once again I seek your kind help to retrieve the data using shape file. I am using the mask to extract the data using shape file as follows;
mask = reshape(isinterior(polygon, lon(:), lat(:)), [length(lat), length(lon)]);
% Convert the logical mask to uint16 format
maskConverted = uint16(mask);
% Apply the converted mask to the raster
imgMasked = img .* maskConverted;
The size of imgMasked is same as that of img(5490 5490 7). However, it contains non zero data over shape file area and zeros over rest of the image data. I wanted to retrieve the non zeros data from imgMasked as per the size of the shape file. I request you to please suggest me how to retrieve the desired data from imgMasked. I have kept the imgMasked.mat file on following google link;
I request you to please have a look on it and suggest me how to get required data from imgMasked.
I would be highly obliged to you.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
参考
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)
