주요 콘텐츠

이 페이지는 기계 번역을 사용하여 번역되었습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

안전성 분석 관리자 링크 정보를 Excel로 내보내기

이 예제에서는 안전성 분석 관리자 스프레드시트의 링크 정보를 프로그래밍 방식으로 Microsoft® Excel®로 내보내는 방법을 보여줍니다. 링크 정보를 만들고 저장한 후 해당 정보를 두 가지 형식으로 Excel로 내보냅니다. 한 파일에는 스프레드시트와 링크 정보가 두 개의 Excel 시트로 포함되고, 다른 파일에는 두 정보가 하나의 시트로 결합됩니다.

스프레드시트 열기

안전성 분석 관리자 스프레드시트 myLinkTable.mldatx를 열고 Spreadsheet 객체를 가져옵니다. 표에는 셀 간의 링크 그룹이 포함되어 있습니다.

safetyAnalysisMgr.openManager
fileName = "myLinkTable.mldatx";
spreadsheetObject = safetyAnalysisMgr.openDocument(fileName);

The Safety Analysis Manager spreadsheet, myLinkTable.mldatx. The table has three rows and six columns. The columns have unique names. Five of the cells have Requirements Toolbox links.

셀 값 및 링크 정보 가져오기

먼저, 셀과 링크 정보를 가져와 MATLAB® 테이블에 저장해야 합니다. 셀 값과 링크를 위해 안전성 분석 관리자 스프레드시트와 동일한 차원의 셀 배열을 만듭니다.

spreadsheetValuesCells = cell(spreadsheetObject.Rows,...
    spreadsheetObject.Columns);
spreadsheetLinksCells = cell(spreadsheetObject.Rows,...
    spreadsheetObject.Columns);

스프레드시트의 값으로 테이블 제목을 설정하기 위해 getColumnLabels 함수를 사용하여 해당 값을 가져옵니다.

columnLabels = getColumnLabels(spreadsheetObject);

getCellgetLinks 함수를 사용하여 셀 값과 링크 정보를 가져와 이를 안전성 분석 관리자 스프레드시트와 동일한 차원의 셀 배열에 저장합니다.

for row = 1:spreadsheetObject.Rows
    for col = 1:spreadsheetObject.Columns
        spreadsheetCellObject = ...
            getCell(spreadsheetObject,row,col);
        spreadsheetValuesCells{row,col} = ...
            spreadsheetCellObject.Value;
        spreadsheetLinksCells{row,col} = ...
            getLinks(spreadsheetCellObject);
    end
end

다음으로, 안전성 분석 관리자 스프레드시트 셀 값의 셀 배열 spreadsheetValues를 테이블로 변환합니다.

spreadsheetValues = cell2table(spreadsheetValuesCells,...
    VariableNames=columnLabels);

셀 배열의 정보를 서식 지정합니다. 링크 정보가 포함된 셀 배열의 각 셀에 formatLinksInfo 헬퍼 함수를 적용합니다. 서식이 지정된 셀 배열을 테이블로 변환합니다.

spreadsheetLinksInfoCells = cellfun(@(x) formatLinksInfo(x),...
    spreadsheetLinksCells);
spreadsheetLinksInfo = array2table(spreadsheetLinksInfoCells,...
    VariableNames=columnLabels);

스프레드시트 값과 링크를 별도 시트로 내보내기

첫 번째 시트에 스프레드시트 값을 쓰고 두 번째 시트에 링크 정보를 씁니다. Excel 파일을 만들고 writetable 함수를 사용하여 시트에 씁니다. Excel 파일을 demoSpreadsheetSeparate.xlsx라는 이름으로 저장합니다. Excel 파일에 시트를 추가했기 때문에 MATLAB는 경고를 생성합니다. Excel 파일에 테이블을 저장하기 전에 경고를 끕니다.

warning("off","MATLAB:xlswrite:AddSheet");
writetable(spreadsheetValues,...
    "demoSpreadsheetSeparate.xlsx",Sheet=1);
writetable(spreadsheetLinksInfo,...
    "demoSpreadsheetSeparate.xlsx",Sheet=2,AutoFitWidth=true);

Excel 파일을 열면 파일에 두 개의 시트가 있습니다. 첫 번째 시트에는 안전성 분석 관리자 스프레드시트 셀 값과 열 이름이 포함되어 있습니다.

The first sheet in Excel. The sheet contains the Safety Analysis Manager spreadsheet values.

두 번째 시트에는 각 셀의 링크 정보가 포함되어 있습니다. 빈 셀은 안전성 분석 관리자의 연관된 셀에 링크가 없음을 나타냅니다.

The second sheet in Excel. The sheet contains the Safety Analysis Manager spreadsheet link information. The link information is organized in the same orientation as the cell values.

스프레드시트 값과 링크를 하나의 시트로 내보내기

값과 링크를 하나의 시트로 내보낼 수도 있습니다. 이 예에서는 안전성 분석 관리자 스프레드시트의 각 열을 링크 정보를 설명하는 열로 내보냅니다. 먼저, 원래의 안전성 분석 관리자 스프레드시트와 같은 행 개수를 갖도록 출력 셀 배열을 초기화하여 결함 및 링크 정보를 위한 다른 열을 만듭니다. 그러나 열 개수는 두 배입니다.

outputNumRows = spreadsheetObject.Rows;
outputNumCols = spreadsheetObject.Columns*2;
spreadsheetValuesAndLinksCells = cell(outputNumRows,outputNumCols);
outputColConfig = cell(1,outputNumCols);

다음으로, 데이터를 열에 할당합니다. 셀 값을 홀수 열에 할당하고 링크 정보를 짝수 열에 할당합니다.

for col = 1:length(spreadsheetValuesAndLinksCells)
    if rem(col,2) == 1 % Cell value columns
        inputCol = (col+1)/2;
        spreadsheetValuesAndLinksCells(:,col) = ...
            spreadsheetValuesCells(:,inputCol);
        outputColConfig{col} = columnLabels{inputCol};
    else
        inputCol = col/2; % Link value columns
        spreadsheetValuesAndLinksCells(:,col) = ...
            spreadsheetLinksInfoCells(:,inputCol);
        outputColConfig{col} = append(columnLabels{inputCol},' Links');
    end
end

그런 다음 셀 배열을 열 이름이 있는 테이블로 변환합니다.

spreadsheetValuesAndLinks = cell2table(...
    spreadsheetValuesAndLinksCells,VariableNames=outputColConfig);

마지막으로 demoSpreadsheetTogether.xlsx라는 이름의 Excel 파일에 테이블을 씁니다.

writetable(spreadsheetValuesAndLinks,...
    "demoSpreadsheetTogether.xlsx",AutoFitWidth=true);

Excel 파일을 열면 셀 값과 링크 정보가 해당 파일의 시트 하나에 모두 포함되어 있습니다. 예를 들어, 함수 이름 열에는 셀 값이 표시되고, 함수 이름 링크 열에는 셀의 링크 정보가 표시됩니다.

The combined Safety Analysis Manager cell value and link information Excel file. The image shows only the first four columns of the sheet.

이 패턴은 안전성 분석 관리자 스프레드시트의 각 셀에 대해 반복됩니다.

헬퍼 함수

formatLinksInfo 함수는 cellLinks의 링크를 읽고 링크 정보를 문자형 벡터로 구성된 셀형 배열로 출력합니다.

function linkInfo = formatLinksInfo(cellLinks)
    inLinks = cellLinks.inLinks;
    outLinks = cellLinks.outLinks;
    linkInfo = "";
    for inLinkID = 1:numel(inLinks)
        if inLinkID > 1
            linkInfo = append(linkInfo,newline);
        end
        linkInfo = linkInfo + inLinks(inLinkID).Type + ...
            " " + inLinks(inLinkID).Description;
    end
    for outlinkID = 1:numel(outLinks)
        if numel(inLinks) + outlinkID > 1
            linkInfo = append(linkInfo,newline);
        end
        linkInfo = linkInfo + outLinks(outlinkID).Type + ...
            " " + outLinks(outlinkID).Description;
    end
    linkInfo = cellstr(linkInfo);
end

참고 항목

도움말 항목