이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Writing multiple structures to user specified excel file
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a variable amount of structures with 4 fields, each containing 1x1 blocks of data. How would I go about writing multiple structures to a user specified excel file?
댓글 수: 2
채택된 답변
Azzi Abdelmalek
2016년 4월 5일
편집: Azzi Abdelmalek
2016년 4월 5일
load Example
v=[struct2cell(Data1.data)]
out=v(:,:)'
xlswrite('file.xlsx',out)
댓글 수: 18
Ibro Tutic
2016년 4월 5일
What if I have multiple structures to consider? Essentially the program allows calculations to be ran against user specified mat files that contain damage information, for each file they select, another structure is created. What I need it for every structure to be outputted to the same, user-specified excel file.
Azzi Abdelmalek
2016년 4월 5일
You need to provide more details, how many fields for each data? do you want to store the data in different sheets?
Ibro Tutic
2016년 4월 5일
The data is exactly the same as that example, but instead of being 10x4, it might be 200x4, or 150x4, but the heading and everything is exactly the same. The data does not need to be stored in different sheets. Also, the PINs are unique, not in numerical order like I had put in the example (not sure if it's confidential information or not). Just imagine having n structures within one structure, with data corresponding to PIN, Displacement, Damage, and hours in each of the structures.
Ibro Tutic
2016년 4월 5일
Well, the data is created within the program and stored as a variable, so it is all in one file.
Azzi Abdelmalek
2016년 4월 5일
You have to make things clear. all your variables are stored in one mat file? do you mean that you are creating several variables with different names? If it's in the program, can you show us the part of your program that are creating these variables, because it's a bad idea to create different variables names, when you can store all your data in one variable
Ibro Tutic
2016년 4월 5일
The code is below. The program reads a mat file, copies the data to a structure, reads another mat file, copies similar data to another structure, and so on.
Essentially, if the user selects two mat files to be processed, two structures would be created. I then need these two structures to be exported to an excel spreadsheet, the same spreadsheet, on the same page.
filepath1 = 'C:\Users\it58528\Desktop\13.5 Stuff\13.5 Mat Files\';
filename = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on');
for n=1:length(filename)
data1{n} = load([filepath1 filename{n}]);
end
for i=1:length(data1)
for j=1:length(data1{i}.PIN)
Data(i).data(j).PIN = data1{i}.PIN(j).PIN;
Data(i).data(j).hours = data1{i}.PIN(j).hours;
Data(i).data(j).Disp = data1{i}.PIN(j).Displacement;
Data(i).data(j).damage = sum(sum((data1{i}.PIN(j).LoadProfilePercent.*DamagenewTable),1));
end
end
Azzi Abdelmalek
2016년 4월 5일
편집: Azzi Abdelmalek
2016년 4월 5일
filename = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on');
for n=1:length(filename)
data1{n} = load(filename{n});
end
out=[];
for i=1:length(data1)
v=[struct2cell(data1{i}.Data1.data)]
out=[out;v(:,:)']
end
xlswrite('file.xlsx',out)
Ibro Tutic
2016년 4월 5일
That works, but it seems to be overwriting previous entries, how would I get around that?
Azzi Abdelmalek
2016년 4월 5일
maybe you have used my code before editing it. copy it now and try again
Ibro Tutic
2016년 4월 5일
Well, here is the code I adapted from yours.
for i=1:length(data1)
v=[struct2cell(Data(i).data)];
out=[v(:,:)'];
end
So what it does is it goes through the structure Data(1).data, converts it to a cell array, cleans up the formatting, and is stored in the variable 'out'. Now when it moves on to the second part of the structure, Data(2).data, the data from the first structure (Data(1).data) are overwritten, leaving only data from Data(2).data in the cell array. What I need is for them both to be in the same table.
Azzi Abdelmalek
2016년 4월 5일
This is not my code, What I've posted is:
out=[];
for i=1:length(data1)
v=[struct2cell(data1{i}.Data1.data)]
out=[out;v(:,:)']
end
Ibro Tutic
2016년 4월 5일
Your code attempts to access a non-existent structure, I simply modified it so it does what I need it to do. The issue is still the same, the data from one structure is getting over written, but I need all the data to be in a single variable, 'out'.
Ibro Tutic
2016년 4월 5일
filepath1 = 'C:\Users\it58528\Desktop\13.5 Stuff\13.5 Mat Files\';
filename = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on');
for n=1:length(filename)
data1{n} = load([filepath1 filename{n}]);
end
for i=1:length(data1)
for j=1:length(data1{i}.PIN)
Data(i).data(j).PIN = data1{i}.PIN(j).PIN;
Data(i).data(j).hours = data1{i}.PIN(j).hours;
Data(i).data(j).Disp = data1{i}.PIN(j).Displacement;
Data(i).data(j).damage = sum(sum((data1{i}.PIN(j).LoadProfilePercent.*DamagenewTable),1));
end
end
for i=1:length(data1)
v=[struct2cell(Data(i).data)];
out=[v(:,:)'];
end
Azzi Abdelmalek
2016년 4월 5일
You did not look at my code!
out=[]
for i=1:length(data1)
v=[struct2cell(Data(i).data)];
out=[out;v(:,:)'];
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
